Awake() Function in Unity

Notes:

Awake() Function in Unity:

W.K.T. In Unity everything is an object. Because everything in Unity inherits from a root base class called Object. When you run a game application, first scene of the game is loaded.
All objects of the scene are created and initialized first.

After all objects are created and initialized; Awake method of Unity Scripts is called and executed.

MonoBehaviour - Awake Event Method:

1. objects / instances are created and initialized

Awake method: is an initialization method
- is called first
- is called only once in a script life cycle
- is executed even if script is not enabled

- is best for initializing the current object’s properties or variables; which do not depend up on other object’s property or variable value.

- if you want to initialize the current object’s properties or variables, which depend up on other object’s property or variable value, then you must initialize them inside Start method because Start methods are called after all Awake methods.

Ex: If you want to initialize the player velocity same as enemy velocity, then initialize enemy velocity inside Awake method of enemy’s script and initialize player velocity inside Start method of player’s script because here player’s velocity property depends upon enemy’s velocity property value.

Note: Each object’s Awake() is called in a random order i.e. there is no guarantee in order of executing Awake(); Hence it is recommended to avoid code in Awake() which relies on other object’s property or variable value.

Example Code:

void Awake()
{
Debug.Log ("Awake");
}