Unity Awake vs Start: What is the Difference?

Unity has a lot of predefined functions that you can use in your game scripting. Awake and Start are two of such functions. But, there is so much confusion on these functions that Unity Awake vs Start is one of the most search terms online. In this post we will see how these functions work and which one to be used where. You can also check out more Unity tutorials here.

Unity Awake vs Start

Let’s Start with what Unity says

Awake ()

Awake is called when the script instance is being loaded.Awake is called either when an active GameObject that contains the script is initialized when a Scene loads, or when a previously inactive GameObject is set to active.

Unity documentation

Start()

Start is called on the frame when a script is enabled just before any of the Update methods are called the first time.

Unity documentation

Now let’s try and understand what they say.

Awake function in a script of a gameobjects is called when the scene containing the gameobject is loaded. The only time Awake is not loaded is when the gameobject is disabled.

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

Common thing about Start and Awake function is both these functions get called only once and both the functions are called after object initialization. So, it is safe to check for other gameobjects in the scene in both Awake and Start.

Unity Awake vs Start with examples

Testing Awake()

Let’s take 3 Gameobjects in a scene g1,g2,g3. I will add a script to each game object with an Awake and a Start function. Inside all these functions I will add an debug log. Here is the sample code.

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class test3 : MonoBehaviour
 {
     void Awake()
     {
         Debug.Log ("Awake1 called");
     }
    
     void Start()
     {
     } 
 }

Scenario one: All gameobjects and scripts are enabled

Unity output 1

As you can use when the game scene was loaded Awake() of all the gameobjects were called.

Scenario Two: All scripts in Gameobjects are disabled, but the gameobjects are enabled.

Unity output 2

Even when the scripts were disabled, their Awake() function was called.

Scenario Three: g1 and it’s script are enabled, g2 script is disabled and g3 gameobject is disabled

Unity output 3

Awake of g1 and g2 got called but g3 was not called. Now let’s enable g3. As soon as I enabled g3 the Awake function gets called.

Testing Start()

I am going to use the same setup and scenario which was used for testing Awake. I will shift the log statement to start function.

Scenario One: All gameobjects and scripts are enabled

Unity output 4

All start function gets called on scene load.

Scenario Two:All scripts in Gameobjects are disabled, but the gameobjects are enabled.

Unity output 5

None of the start functions are called in this case.

Scenario three: g1 and it’s script are enabled, g2 script is disabled and g3 gameobject is disabled

Unity output 6

Start function of g1 is called but g2 and g3 are not called. When I enable the script attached to g2 the Start function gets called.

Still confused? Let’s see an Analogy

Take a team for 20 people being considered for a football match. Out of these 20, only 15 will be in the squad and only 11 will play.

Lets do an Unity Awake vs Start comparison with these players. Assume that an Awake() and a Start() function is attached to these players.

Players who are not in the squad are like disabled gameobjects. Their Awake() function never gets called.

When the squad is announced the Awake() function of the 15 players in the squad gets called and when the match starts only the playing 11’s Start function gets called and when a player is substituted, at that time the Start function of the player entering the ground is called.

Hope this has cleared your doubts on Unity Awake vs Start functions. If you have any other queries, feel free to leave a comment below.

Lets see where to use them in your Game

  1. Initializing public variable- Better done in Awake , as other gameobject 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.

Leave a Reply

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

%d bloggers like this: