Unity execution order in Awake, OnEnable and Start of

Concept analysis

void Awake) (: for any variable is initialized before the game starts and the state of the game is called only once in the script instance life cycle, can not do coroutine. Awake function is called after all objects instantiated, and therefore can be safely with other objects such as communications or inquiries; at the same time, Awake function is called before any Start functions, so we can specify the order of initialization script. Awake function calls between objects in no particular order provisions, it is possible to take advantage of Awake function to create references between the script, and then use the Start function to pass information back and forth. (For C #, Boo user, when the assembly is configured serialized state is not defined, should be used instead of Awake constructor to initialize).

void Enable ():  Only object is available to be called the active state, is called after the object is available, this usually occurs when MonoBehaviour instance is created, you can not do coroutine. 

void Start ():  If the script instance is enabled, the Start function is called before the first frame updates, is called only once in the script instance life cycle. Whether or not script enabled, Awake functions call; if script disabled during the initialization, the Start function will not be called Awake and function in the same frame. Awake function and OnEnable function must be before the Start function call (benefits are: object A instantiation code depends on Object B is instantiated, instantiation of B should be completed in Awake stage, examples of A is completed at the Start stage). Note: For added to the scene of objects, such as Update function is called before all scripts Start function will be called in any script. However, during the gameplay is instantiated objects, it can not be forced to follow the rules of the.

Execution order

In the same script in their order of execution is Awake () -> Enable () -> Start (). Of course, the same script here that is mounted on the same Object, the same script to mount between the different scripts and different Object is not true.

First Awake and OnEnable certainly prior to the Start execution.

So has the relationship between Awake and OnEnable it?

See the following example, a script to mount the same on three different objects.

Print results:

 

Conclusion: The execution order between different scripts (or objects) of Awake and OnEnable are not related.

Guess you like

Origin blog.csdn.net/qq_38721111/article/details/90270235