Unity have a lot of special event functions that gets called automatically by the engine. For example Start and Update function is added to every script by default.
In this post I want to talk about the difference between two similar functions that can easily be mixed up with each other, Start and Awake.
Start vs Awake
Start and Awake function work also identical, both are called near the beginning with Awake being called earlier. However there is one key difference between the two, on Start will not be called in the script component is disabled.
Usually Unity coders will use Awake to create component references and initializing variables. While Start will be used to access those data to avoid errors.
Here is what the start and Awake function looks like in Unity.
// Start is called before the first frame update
// Use this for initialisation
// Awake is called even if the script is disabled.
Awake is called only once on each script, after all the objects are initialized. It is called when the script is first loaded or when an object it’s attached to is instantiated.
This makes it good to create references to other game objects and components that will be used later on.
However every object’s Awake is called on randomly, which means you cannot guarantee that one object’s Awake function will be called before another object.
Therefore if you need to access some variables in another object’s awake function, it may or may not exist. This is why we have the Start function.
Start function works the same way as Awake but it cannot be called if the script is disabled. This also means that you can manually delay Start by disabling the game object and then enable it through another piece of code.
I hope you will find this information useful and how to use the two function is completely up to you.