Unity Awake vs Start: Understanding the Differences

Awake() and Start() are the initialization functions for any script in Unity. They are the very first functions that are called in a script. The order of execution may differ but Awake() and Start() can be used interchangeably in most cases. In this article, we will see the difference between how Awake() and Start() work and how to use them effectively.

The main difference between the Awake() and Start() functions is how the functions are called depending on the script’s and Game object’s status. Awake is called when the script of the object is added to the scene and Start function is called when the script is enabled.

Unlike the Update methods, Start and Awake methods are called only once per lifetime of the script.

Let’s understand Awake() vs Start() with an example

Let’s add a cube, a sphere and a capsule to our scene. And let’s attach the same script to all three. The script has a Start and an Awake function with debug logs.

The script will debug log the game object’s name and say its Awaked when the Awake function is called and will say Started if the Start function is called.

Here is the script

using UnityEngine;

public class StartvsAwake : MonoBehaviour
{
    void Awake()
    {
        Debug.Log(this.gameObject+" is Awaken.");
    }
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(this.gameObject+" is Started.");        
    }

}

We will put all three game object in different states.

  1. The cube game object is set active and the script is enabled.
  2. The sphere game object is set as inactive.
  3. The capsule game object is active but the script is disabled.

Now if we play the game, here is the output logs

Awake is called on the Cube and capsule. Start is called on the cube gameobject.

As you can see the Awake() of both Capsule and cube is called first and then the Start() of the cube is called.

Now if we enable the script component of the capsule, then the Start() function of capsule is called. Then if we enable the sphere game object, then the Awake() and Start() of the sphere are called.

Start of the capsule is called then Awake and start of the sphere are called.

Unity Execution Order

Awake and Start are what Unity calls the event functions, but they are not the only ones. The other major event function that you would have hear of is the OnEnable and the Update functions. Here is the order in which these functions are called.

Order of execution of the Awake, OnEnable, Start, FixedUpdate and Update functions.

Awake Comes first

In the order of execution, the Awake function comes before the Start function. That is if you have both Awake and Start in the same script then the code in Awake gets executed before Start. This can be useful in scenarios where you want some variable to be initialized before instantiating the player prefab.

In fact, all the Awake functions in the scene are called before the first Start function is called. Taking the same example of the cube, sphere and the capsule. Let’s enable all the game objects and the scripts. Now if play the game, here is the output.

All Awake functions are called before the first start function.

Start() can be delayed but not Awake()

One unique feature of the Unity Start function is that it can be used as a coroutine. To use the Start function as coroutine you need to specify the Start function to return an IEnumerator and use the yield return statement.

Unlike Start, the Awake function cannot be delayed. It will be called as soon as the Gameobject gets active in the scene. Here is a sample script to delay the Start function.

 IEnumerator Start()
 {
     yield return new WaitForSeconds(5f);
     //rest of the code
 }

Start can also be used as Async function to delay it.

Does SetActive() call Awake and Start?

As we saw in the above scenarios, both Awake and Start are not called if the Gameobject is not active. But when you set the Gameobject to active using the SetActive function, Awake is called irrespective of whether the script is active or not. Start is called only if the script is active.

Should I use Awake or Start method?

In most of the cases it doesn’t matter but a small mistake like disabling the script can differ the Start function from being called and sometimes Awake will be called even when you thought, you disabled the script. Here are some best practices to use Awake and Start.

  1. To assign initial values to public variable- Better done in Awake, as other Gameobjects might reference them.
  2. Checking for Gameobjects and their components- can be done in both Awake and Start.
  3. Initializing Gameobject specific variable like health- Better to be done in Start.
  4. Need delayed Initialization- Can be done in Start, Awake call cannot be delayed.
  5. Set all player ammo to zero on scene load- can be done in Awake. Ammo will be set to zero even if the script is disabled.

Unity Awake vs Start Summary

FeaturesAwake()Start()
Order of ExecutionFirst Function to be calledStart is called after the Awake and OnEnable()
Called when Gameobject is active and script is disabledYesNo
Called when Gameobject is disabledNoNo
Can be delayedNoYes
Can be used as a coroutineNoYes
Number of times it can be called in the script’s lifetimeOnceOnce

Awake function in a script of a Gameobjects is called when the scene containing the Gameobject is loaded even if the script component is disabled. The only time Awake is not loaded is when the Gameobject is disabled.

The Start function is called when the script is initialized. If you keep the script disabled then Start will not be called.

We have tested a few more scenarios in the video below.

Hope this clears the difference between Start and the Awake functions. If you have any other questions feel free to leave them in the comment box below.

2 thoughts on “Unity Awake vs Start: Understanding the Differences”

  1. So if I have a disabled script attached to an active gameobject with an Awake function then it will be called. This was the mistake in my code. I searched for so long. Thank you.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading