Search Unity

scripting execution order, ...

Discussion in 'Scripting' started by sdgd, Jan 3, 2014.

  1. sdgd

    sdgd

    Joined:
    Jan 15, 2013
    Posts:
    81
    I was thinking how should I add knowledge here:

    http://forum.unity3d.com/threads/2606-Docs-on-calling-order-in-MonoBehaviour

    Code (csharp):
    1.  
    2.     void Awake(){
    3.         Debug.Log("0");
    4.     }
    5.     void OnEnable(){
    6.         Debug.Log("1");
    7.     }
    8.     public void test(){
    9.         Debug.Log("2");
    10.     }
    11.     void Start(){
    12.         Debug.Log("3");
    13.     }
    14.  
    if you are wandering what test is it is this:

    Code (csharp):
    1.  
    2.     GameObject CheckPointGO = Instantiate(PrefabTestGO);;
    3.     CheckPointGO.GetComponent<TsetScript>().test();
    4.  
    when this is called you'll get:
    0,1,2,3 that's the execution order

    it's useful to know why setting Awake or OnEnable if you are immediately calling the function, ... before the end of the frame
     
  2. ProFlares

    ProFlares

    Joined:
    Nov 1, 2013
    Posts:
    154
    Take a look at this diagram of the mono behaviour life cycle. It is a bit old now but should give you a good idea whats going on.

    View attachment 80973
     
  3. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    This has been in the regular documentation for a very long time.

    http://docs.unity3d.com/Documentation/Manual/ExecutionOrder.html

    An overview of the order of execution for all event calls, for all running scripts, can be summarized by the following pseudo-code:

    Code (csharp):
    1.  
    2. /**********************************************************
    3. All Awake calls
    4. All Start Calls
    5. while (stepping towards variable delta time)
    6. {
    7.    All FixedUpdate functions
    8.    Physics simulation
    9.    OnEnter/Exit/Stay trigger functions
    10.    OnEnter/Exit/Stay collision functions
    11. }
    12. Rigidbody interpolation applies transform.position and rotation
    13. OnMouseDown/OnMouseUp etc. events
    14. All Update functions
    15. Animations are advanced, blended and applied to transform
    16. All LateUpdate functions
    17. Rendering
    18. ***********************************************************/
    19.  
     
  4. sdgd

    sdgd

    Joined:
    Jan 15, 2013
    Posts:
    81
    very intereesting didn't know that before @ProFlares

    but still both of you2 are missing 1 thing that I wanted to share, ...

    between

    void OnEnable(){}

    and

    void Start(){}

    there's function call
     
  5. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    You called the test() function. Nothing can stop you from calling a class object once it is instantiated.

    Code (csharp):
    1.  
    2.     GameObject CheckPointGO = Instantiate(PrefabTestGO);
    3.     CheckPointGO.GetComponent<TsetScript>().test();
    4.  
    The order of events of this code is this (I think):

    Code (csharp):
    1.  
    2.    GameObject CheckPointGO = Instantiate(PrefabTestGO);
    3.    //Inside the Instantiate() call...
    4.    //{
    5.    //  A copy of PrefabTestGO is created and the MonoBehaviour class objects associated with the game object are instantiated.
    6.    //  If PrefabTestGO.enabled == true
    7.    //  {
    8.    //      Awake() is called on the instantiated TsetScript object
    9.    //      If TsetScript.enabled of PrefabTestGO == true
    10.    //          OnEnable() is called on the instantiated TsetScript object
    11.    //   }
    12.    //}
    13.    CheckPointGO.GetComponent<TsetScript>().test();
    14.    // Start() is called at the beginning of the next iteration of the game loop, if the component and object are active and enabled.
    15.    // This will be followed by the first call into the component's Update() method.
    16.  
     
    Last edited: Jan 4, 2014
  6. sdgd

    sdgd

    Joined:
    Jan 15, 2013
    Posts:
    81
    yes @Brian Stone my debugs says it works that way

    I just didn't know how to share this knowledge so I made this post.