So far, we have used Start to get components and save them in fields. I am curious from our three options (Awake, OnEnable, Start) when do we use which and why?
Hi moment,
Welcome to our community!
First of all, we need to know that Unity methods with the same name get executed in a random order. This means that, if we have two Start() methods, we cannot tell if our “first” Start() method gets called first.
If we want to ensure that code gets executed before our “first” Start() method gets called, we have to execute that code in Awake.
OnEnable gets called whenever a component becomes enabled and activate.
Do you know this flowchart?
You can find more detailed information on the methods in the Unity API and manual.
I hope this helped.
See also:
- Forum User Guides : How to mark a topic as solved
@moment - The Order of Execution in Unity is important to know in the link @Nina provided above as it shows the flow of how Unity executes methods (or functions).
Awake () would be the very first method that can run in this order as per the diagram.
The next would be OnEnable()
Both of these methods get loaded as soon as the Scene loads and because of this they would be placed in your script above the Start() and Update() methods.
Start() loads ONCE for any given script as part of the Initialization
Update() loads every ‘tick’ (constantly), which is used for Game Logic
I hope this adds a little more to @Nina 's already excellent explanation.
Best of luck!
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.