Awake() vs Start() in Unity

Using Awake and Start functions in your game script can be confusing as these functions can replace each other in most cases. But there are some scenarios where only Awake function can be called and vice versa. In this post, we will see how these functions work and which one is to be used where. In all my years of game development, I didn’t need to use the Awake function much. Most of my game scripts comprise the Start function. Awake is used very rarely in scripts and can be very useful sometimes.

The functional difference between Awake() and Start() is based on how the scripts and Gameobjects are called in the scene. Awake is called when the script of the object is added to the scene and Start function is called when the script is enabled. This is just a basic idea of when Start and Awake functions are called.

Unity Awake and Start basic comparison table

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

Let’s see what Unity says

Unity Awake () function

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

Unity Start() Function

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.

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

The common thing about Unity 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 testing with live code samples

Testing Unity Awake function

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 a debug log. Here is the sample code. The debug log contains the number based on the gameobject, i.e in g1, it’s awake1, and in g2 it’s awake 2 so on.

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.

In this scenario, we will keep all the Gameobjects and their respective scripts enabled. Then let’s hit play and see the debug log.

When the game scene was loaded Awake() of all the Gameobjects are called. All three debug logs get displayed on the console.

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

We will keep the Gameobjects active in the hierarchy but disable the script attached to all the Gameobjects.

Unity awake with scripts disabled.

Even when the scripts were disabled, their Awake() function was called. This shows that irrespective of the status of the script the awake function is called if the Gameobject is active in the hierarchy.

Scenario Three: Mixing things up to see the result.

g1 and its script is enabled, g2 script is disabled in inspector and g3 Gameobject is disabled in the hierarchy.

Unity awake with some scripts and some game objects disabled.

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.

The conclusion from the above experiment. Awake function will be called at scene load if the object is set active even if the script is disabled.

Testing Unity Start function

I am going to use the same setup and scenario which was used for testing Awake. I will shift the log statement to the Start function. Same Gameobject g1,g2,g3 are used for testing the Start function.

Scenario One: All Gameobjects and scripts are enabled

Unity start with all enabled

All start function gets called on scene load. You can see all the debug log appearing on the console window.

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

Unity start with scripts disabled

None of the start functions are called in this case. Unlike the Awake function, the Start function is not called if the script is disabled.

Scenario three: g1 and its script are enabled, g2 script is disabled and g3 Gameobject is disabled

Unity start with some scripts enabled and some game objects enabled.

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.

This makes it clearer that the Awake function is more Gameobject dependent and the Start function is more script dependent.

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.

For Example, if you need to set the inventory items before the player gets spawned then you can do it in the Awake 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
 }

Does SetActive() call Awake and Start?

As we saw in the above scenarios, both Awake and Start are not called if the Gameobject is set to 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?

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 is the best way to use Awake and Start.

  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.

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. You can also check out more Unity tutorials here.

2 thoughts on “Awake() vs Start() in Unity”

  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.