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#.































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


int score = 10 - 5; // 5
int age = score - 1; // 4
int total = score - age; // 1
score -= 1; // 4
score--; // 3


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


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


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)


bool result = 10 > 3; // true
result = 3 > 10; // false


bool result = 10 < 3; // false
result = 3 < 10; // true


bool result = 10 == 10; // true
result = 10 == 3; // false


bool result = true && true; // true
result = true && false; // false
result = false && true; // false
result = false && false; // false


bool result = true || true; // true
result = true || false; // true
result = false || true; // true
result = false || false; // false


bool test = false; // false
test = !test; // true


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"


string name = "Jon";
string firstLetter = name.Substring(0, 1); // "J"
string secondLetter = name.Substring(1, 1); // "o"
string lastTwoLetters = name.Substring(1, 2); // "on"


string fruit = "apple";
int letterCount = fruit.Length; // 5


string fruit = "apple";
bool test = fruit.Contains("a"); // true
test = fruit.Contains("app"); // true
test = fruit.Contains("b"); // false


int mod = 10 % 3; // 1


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


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


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


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


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


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


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


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


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


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


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


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


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


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

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 *