C# for Unity

Functions

Functions are collections of code that compare and manipulate variables. They start with an uppercase letter.

Awake()
Start()
Update()
FixedUpdate()
LateUpdate()

Learn more about MonoBehaviour functions.

Awake

Is called only once when the GameObject with that component is instantiated.

Start

Like Awake, Start will be called if a GameObject is active, but only if the component is enabled.

Update

Is called once per frame, like animations.

FixedUpdate

When you want to do physics work.

LateUpdate

Is a function that’s similar to Update, but LateUpdate is called at the end of the frame




Classes

Classes are collections of variables and functions. For example, this script is a class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour {
	// ...
}

( ! ) The class name must be the same as the script's file name, else it won't work.


Unity Documentation




Variables

Variables start with a letter. They’re like a box that holds something for us to use.

The most used variable types:

public int Number = 12345;
public float Decimals = 1.1;
public string text = "use quotes";
public bool ImHappy = true;

// the most common variables in Unity

public GameObject MyCube;
public transform MyTransform;
private MeshFilter MyMeshFilter;
private BoxCollider MyBoxCollider;
private MeshRenderer MyMeshRender;
private MeshCollider MymeshColider;

You’ll see that you can access and see the variables declared as public in the Inspector, but you can’t see the private one.




Operators

C# provides a bunch of operators like arithmetic operators perform arithmetic operations with numeric operands and Booleans.Operators library

Conditional Operators


Op Description
! Logical NOT
` `
&& Logical AND (short circuit)
` `
& Logical AND —evaluate both sides
? Conditional

Relational Operators


Op Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equality
!= Inequality

Math operators



int a = 7;  // use double instead float to add decimals 
int b = 2;

Void Start () {
   Debug.log (a + b + "addition");
   Debug.log (a - b + "substraction");
   Debug.log (a / b + "division");
   Debug.log (a * b + "multiplication");
   Debug.log (a % b + "Modulo"); // = 1 why?  7/2  
   Debug.log (a++ + "add one");
   Debug.log (b-- + "substract one");

Functions can do calculations and then return a value. You can ask a function to do something, process the information, then return an answer. If you use the type "void", then they are not returning anything.




If and Else

Self-explanatory

public int speed;
private int MinSpeed = 10;
private int MaxSpeed = 200;

void update () {
	if (speed > MaxSpeed) {
		Debug.Log("wow! easy, man!");
	}
	else if (speed < MinSpeed){
		Deboug.Log("hey! speed up");
	}
	else {
		Debug.Log("Nice landscape dude");
	}
}



Switch Case

Works similar to if and else

Public float CurrentTime;
private string hours = ("CurrentTime")
   
void Start () {

   switch (hours) {
       
      case 7:
         debug.log (hours + "good breakfast")

         case 12:
           debug.log (hours + "Time to lunch")      

         case 18:
           debug.log (hours + "Time dinner")      

    }
 }



Arrays

An array is a list of similar objects, each objects get a position, starting from 0.

public string[] Fruits = new string [5] 
{"Berry","Grape","Melon","kiwi","Cherry"};
// 0    ,   1   ,   2   ,   3  ,   4    

Asking for array objects

Void start (){

   Debug.Log(Fruits[0]); // output: Berry
   Debug.Log(Fruits[3]); // output: Melon
   Debug.Log(Fruits[5]); // output: Cherry
}

Replacing array objects.


Void start (){

   Debug.Log(Fruits[3]); // output: Melon
   Fruits [3] = "Banana";  //  Melon is now Banana
   Debug.Log(Fruits[3]); // output: Banana 
}



Loops

An atuomatic method to cycle through the array values.

public string[] Fruits = new string [5] 
{"Berry","Grape","Melon","kiwi","Cherry"};

void Start () {
   for (int i = 0; i < Fruits.Length; i++)
   {
      Debug.Log(Fruits[i]);
   }
}

// output: Berry Grape Melon kiwi Cherry



Foreach

public string[] Fruits = new string [5] 
{"Berry","Grape","Melon","kiwi","Cherry"};

void Start () {
   foreach (string Fruit in Fruits)
   {
      Debug.Log(Fruit);
   }
}

Do/While

Do actions while it meet the conditions

public int[] numbers = new int [] 
{2,2,2,2};

int result = 0;
int i = 0; 

void Start () {

   do
      {
         result += numbers[i];
         Debug.log(result);

         i++;
         Debug.Log(i + "i value");    
      }
   while (i < 2);
   Debug.log(result);

}

Searching for array position and object.


public string[] fruits = new string[] 
{"Berry","Grape","Melon","kiwi","Cherry"};

private int i = 0;

void Start () {

   do
      { 
         i++;  
      }
      while (fruits[i] != "kiwi");
   Console.Write(i + " " + fruits[i]);
}

// output: 3 kiwi