- Home /
If I have 2 different scripts both have Awake() on 2 different GameObjects, which Awake() starts first?
How can I know the order of excution of Start() and Awake() and other functions on two different scripts and GameObjects? Also how can i assure that this Awake() starts before any other Awake()?
Answer by RDAVIDCM · Aug 08, 2016 at 01:48 PM
-You should check the unity manual for the order of execution of different events: https://docs.unity3d.com/Manual/ExecutionOrder.html
-Also, there is a way you can setup which scripts have priority (get called first) than others. This is the manual entry for Script Execution Order Settings: https://docs.unity3d.com/Manual/class-ScriptExecution.html
Example:
Suppose you have ScriptA on GameObjectA and ScriptB on GameObjectB, then if you want Awake() to get called first in ScriptA than in ScriptB, you must put ScriptA in the Script Execution Order Settings to have a time that makes the ScriptA to be before of the Default Time band in the seettings window. All scripts are set to be at that default time band, so making ScriptA to be before that default band, will imply that all unity events (update, awake, start, etc..) in ScriptA gets called before than ScriptB unity events, since ScriptB is by default on the default time band.
Note however that if you have ScriptA on GameObject1 and same ScriptA on GameObject2, then the order of execution here is not defined to be first one or the other.
just to be clear, if you dont use the settings for the order of execution, all scripts will have the same default time, and in that case, the order of execution is undefined. This is valid for not only awake, but all unity events. Related questions: http://answers.unity3d.com/questions/60109/order-of-update-execution.html , http://answers.unity3d.com/questions/310706/execution-order-of-multiple-scripts-attached-to-di.html
Answer by Addyarb · Aug 08, 2016 at 01:04 AM
Edit>Project Settings>Script Execution Order
Do i have to put all the scripts on that menu and make the one i want the first, or i can put only the one i want and it will execute before anything else?
Put only the one you want to run first. Unity will run through whatever you put in that a box in order before execute everything else you hadn't place in there in whatever order it feels like it (not really).