Hello everyone, today we will talk about a very common topic about unity’s script lifecycle. We all are familiar with the awake() , OnEnable() and start() method. Awake() and Start() are more common for beginner developers tho. There are differences between these three methods and its important to understand these differences so that we can use those efficiently. Let’s start.
Hopefully we know that unity c# scripts event functions have an execution order. Between Awake , Start and OnEnable the order is like this-
- Awake() : This function is always called before any Start functions and also just after a prefab
is instantiated. (If a GameObject is inactive during start up Awake is not called until it is made active.) - OnEnable() : (only called if the Object is active) This function is called just after the object is enabled. This happens when a MonoBehaviour instance is created, such as when a level is loaded or a GameObject with the script component is instantiated.
- Start() : Start is called before the first frame update only if the script instance is enabled.
Let me write the differences first in general. Awake will be called for the first time when the object is instantiated. It will get called only once in the lifetime. And for start it will get called after awake and onEnable only once in a lifetime. But for OnEnable it will get called everytime when the object is enabled. Means if we disable the object somehow and then re-enable it then the onEnable function will get called. Awake will get called regardless the scripts state, I mean weather the script it enabled or not. But start and onEnable will get once only when the script it enabled. For start its only for the first time and for OnEnable its everytime when the script it getting enabled. Lets jump to practical examples.
First create a empty gameobject in the hierarchy and create and attach a script in it.
In our script , simply just add Awake() with a debug.
void Awake()
{
Debug.Log("Awake called");
}
Now lets play the game and we will see the debug in our console. Notice one thing..
There is no script enable/disable option here now. Because as i said before awake don’t depend on the scripts state. It will get called weather the script is enabled or not.
Now let’s add our OnEnable function.
void OnEnable()
{
Debug.Log("OnEnable called");
}
Now if you come back to unity you will notice now we have option to enable/disable the script.
Because, enable depend one the state of the script. It will get called every time when the script is enabled.Now make the script disabled script. Play the game you will now see only one debug in the console. Only for the awake function. Now when u r in the play mode, from the inspector window enable the script. Instantly you will see a debug in the console from the onEnable function.Now again disable and then enable the script from the inspector when you are in play mode. Every time you will enable the script the OnEnable function will get called.
Okay, now add our start function to our code.
void Start()
{
Debug.Log("Start called");
}
Now if you come back to unity and play the game when the script is enabled, you will see total 3 debug in the console. One for awake, one for OnEnable and one for the start function. If we disable the script in the then play the scene then we will see only one debug from the awake function and in the runtime if we enable the script then we will get two new debugs from OnEnable and awake. But next time when you will disable and enable the script (still in the same runtime) only the debug from the onEnable will get called. So, start only get called once when the script is instantiated.
That’s all. We need to understand the difference between awake, OnEnable and start so that we can utilize those properly. As example, suppose you are working with tho scripts. ScriptA and ScriptB. Some objects are instantiated in ScriptA and in ScriptB we need to take the references of those instantiated objects. If we instantiate those objects in Start function of ScriptA and in ScriptB’s start function we search for those objects we will face errors because ScriptB’s start function can run before ScriptA’s start function. But if we instantiate those objects in ScriptA’s awake and search for those in ScriptB’s start then surely it won’t face any issue. This is just an example. In practical cases you will find more issues where you will have to use start and awake properly.
This is all for this article. Hope it will be helpful for beginner unity developers. If you need any other clarification please feel free to leave comment. Will try my best to make you understand that issue. Best of luck