- Home /
Execution sequence of functions?
I want to know when the unity3d application run,
Which functions execute first,and next and so on?For exmaple a script include:
Awake(), Start(), Update(), OnTriggerEnter(),OnMouseOver, and other MonoBehaviour's functions
As I know,the simple sequence is: Awake() --> Start() --> Update()
but I want to know the others funtions' sequence of execution.Like OnMouseOver() and Update(),which first?
Although I can Debug them ,but I want to know how they execute?
And what's more: Different Ojbects add the same component ,Which component's functions execute first? According to What?
Answer by Bunny83 · Aug 04, 2011 at 09:31 AM
http://answers.unity3d.com/questions/topics/execution-order.html
http://unity3d.com/support/documentation/Manual/Execution%20Order.html
http://www.unifycommunity.com/wiki/index.php?title=Event_Execution_Order
I guess you've already read those? Because you can get that information any clearer. You can test it yourself by putting a Debug.Log with a timestamp into each function if you want.
GameObjects are processed in the order they are stored inside the engine. You can't change the order from outside.
The only events that can be ordered are OnGUI with GUI.depth and the rendering of cameras with Camera.depth.
If you need a special order of two scripts you can use Update and LateUpdate to make sure the Update of the other object is already done. If you have more complex event chains you should use your own messaging system.
In addition, with Unity 3.4 you can now control the overall execution order of Start
s and Update
s (for example, if A
is set to execute before B
, then A.Awake is called before B.Awake
, A.Start
before B.Start
, A.Update
before B.Update
, etc.) It can be still a challenge to set this up correctly... for instance the sequence A.Start B.Start B.Update A.Update
is not possible (AFAI$$anonymous$$).
I rather guess Suyuanhan didn't already read those, since it was asked about...
@Statement: Of course, it was a bit ironic... ;)
@Herman-Tulleken: Well, that's a nice new feature but the ordering happens per script / class and not per instance so the execution order of two instances of the same script is still "random".
Oh and Awake is only ordered at scene load since Awake is called from the constructor it'S always called directly when the object is created. Unity just orders the creation of the objects in the scene.