Awake vs Start

Scripting API in Unity provides a set of initialization functions.

Initialization functions are the functions that are called at the start of the script lifecycle.

Unity’s initialization functions are Awake and Start.

Awake vs Start

The differences between Awake and Start are execution order and run conditions.

Awake functions run before the Start and Start functions only run when the script is enabled. Both functions run before the first update method.

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

    void Start()
    {
        Debug.Log("Start runds second");
    }

Start function can be a Coroutine (by replacing void with IEnumerator and adding a yield parameter), but Awake function can not.

    IEnumerator Start()
    {
        //Wait 1 second before running the next code
        yield return new WaitForSeconds(1);

        Debug.Log("Start");
    }

Takeaway

Both functions are useful for initialization purposes (ex. assigning private variables, spawning game objects, etc.), and when used in combination, can help to implement a wide variety of scenarios.

Leave a Reply

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