Understanding Unity – Part 2

The primary language of Unity is C#. In my opinion C# is just like JAVA so if you know that you should be just fine. When you create a script in Unity it automatically inherits from the class MonoBehaviour. If you do not inherit from MonoBehaviour your class becomes a regular old C# class.

MonoBehaviours have a number of methods that you can implement, that are called over the life time of the object. All the way from when the object first gets created to when it is destroyed.

These different methods are executed in a different stages and these stages happen in a certain order. Going from “Initialization” -> “Game Logic” -> “Decommissioning”. I am not including a few of the stages here because they are not fundamental for getting started with unity.

Then the break down of the most important methods in each stage would be as follows:

Initialization:

  • Awake() – This function is called the second that an object is created. “Created” in these sense just means the second that game engine acknowledges its existence.
    • Start() – Similar to Awake() this function is called right before the first frame that is being rendered. You can think of it like just before it appears on screen.

GameLogic:

  • Update() – The Update function holds all your game logic, and gets executed every frame

Decommissioning:

  • OnDestroy() – Gets called just before a object is destroyed. This can come in handy, when you want to say update the score after killing an enemy.

There are also event triggered functions, such as OnCollisionEnter2D that get executed under specific situations. In the next post I will go over these functions, and the 2D physics system in Unity.