Scratch To Unity: Operators

This post provides a handy reference guide to go from Scratch’s “Operators” category of code blocks to C# with Unity.

Code Blocks

Click on the picture of any of these Scratch code blocks to see how to write similar code statements in C#.































[csharp]
int score = 5 + 6; // 11
int age = score + 1; // 12
int total = score + age; // 23
score += 1; // 12
score++; // 13
[/csharp]


[csharp]
int score = 10 – 5; // 5
int age = score – 1; // 4
int total = score – age; // 1
score -= 1; // 4
score–; // 3
[/csharp]


[csharp]
int score = 3 * 5; // 15
int age = score * 2; // 30
int total = score * age; // 450
score *= 3; // 45
[/csharp]


[csharp]
int score = 10 / 3; // 3
double amount = 7.0 / 2.0; // 3.5
int total = score / score; // 1
score /= 3; // 1
[/csharp]


[csharp]
int score = Random.Range(1, 11); // Could be one of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
float time = Random.Range(0f, 1f); // Could be any value between 0 (inclusive) and 1 (inclusive)
float amount = Random.value; // Could be any value between 0 (inclusive) and 1 (inclusive)
[/csharp]


[csharp]
bool result = 10 > 3; // true
result = 3 > 10; // false
[/csharp]


[csharp]
bool result = 10 < 3; // false result = 3 < 10; // true [/csharp]


[csharp]
bool result = 10 == 10; // true
result = 10 == 3; // false
[/csharp]


[csharp]
bool result = true && true; // true
result = true && false; // false
result = false && true; // false
result = false && false; // false
[/csharp]


[csharp]
bool result = true || true; // true
result = true || false; // true
result = false || true; // true
result = false || false; // false
[/csharp]


[csharp]
bool test = false; // false
test = !test; // true
[/csharp]


[csharp]
string name = “Jon” + ” the great!”; // “Jon the great!”
string firstName = “Jon”;
string lastName = “Parham”;
string fullName = string.Concat(firstName, ” “, lastName); // “Jon Parham”
string[] fruits = { “banana”, “grape”, “strawberry” };
string allFruits = string.Join(“, “, fruits); // “banana, grape, strawberry”
[/csharp]


[csharp]
string name = “Jon”;
string firstLetter = name.Substring(0, 1); // “J”
string secondLetter = name.Substring(1, 1); // “o”
string lastTwoLetters = name.Substring(1, 2); // “on”
[/csharp]


[csharp]
string fruit = “apple”;
int letterCount = fruit.Length; // 5
[/csharp]


[csharp]
string fruit = “apple”;
bool test = fruit.Contains(“a”); // true
test = fruit.Contains(“app”); // true
test = fruit.Contains(“b”); // false
[/csharp]


[csharp]
int mod = 10 % 3; // 1
[/csharp]


[csharp]
float result = Mathf.Round(3.2f); // 3
result = Mathf.Round(3.7f); // 4
[/csharp]


[csharp]
float result = Mathf.Abs(-3f); // 3
[/csharp]


[csharp]
float result = Mathf.Floor(3.7f); // 3
[/csharp]


[csharp]
float result = Mathf.Ceil(3.3f); // 4
[/csharp]


[csharp]
float result = Mathf.Sqrt(4f); // 2
[/csharp]


[csharp]
float result = Mathf.Sin(4f); // -0.7568025
[/csharp]


[csharp]
float result = Mathf.Cos(4f); // -0.6536436
[/csharp]


[csharp]
float result = Mathf.Tan(4f); // 1.157821
[/csharp]


[csharp]
float result = Mathf.Asin(0.5f); // 0.5235988
[/csharp]


[csharp]
float result = Mathf.Acos(0.5f); // 1.047198
[/csharp]


[csharp]
float result = Mathf.Atan(0.5f); // 0.4636476
[/csharp]


[csharp]
float result = Mathf.Log10(100f); // 2
[/csharp]


[csharp]
float e = (float)System.Math.E;
float result = Mathf.Pow(e, 2); // 7.389056
[/csharp]


[csharp]
float result = Mathf.Pow(10, 2); // 100
[/csharp]

Links

Here are a few links that are relevant to this material and can help you dive a little deeper if you need more help.

Summary

This should cover all of the code blocks in Scratch’s “Operators” category. If you have any questions about this reference guide feel free to ask below.

If you find value in my blog, you can support its continued development by becoming my patron. Visit my Patreon page here. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *