Search Unity

Scripts, functions and order of call

Discussion in 'Scripting' started by Kenneth, Mar 27, 2008.

  1. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    On
    file:///Applications/Unity/Documentation/ScriptReference/MonoBehaviour.html
    there's a walkthrough of different functions. But what if you make 2 different scripts which both of them has functions of the same name. Which function is run first? Does it depend on which of the scripts was attached to the object first or how does the engine deside it?
     
  2. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    The call order of functions can be found somewhere on the forum or wiki if you search for it. It goes something like:

    All Awake()'s
    All Start()'s
    All Update()'s
    All LateUpdate()'s
    Rendering

    The order of individual calls are non defined. It might apear in the editor to be script first added, but that is not garantueed to be the case when you build.

    Same goes for all other equal named function that the engine executes.

    If you need to get a specfic order of executing of say Start() you need to implement the order yourself by doing some clever waiting and reporting.

    PS: Found the link anyways http://www.unifycommunity.com/wiki/index.php?title=Event_Execution_Order

    Regards,
    Marc
     
  3. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    So although the functions are called in a certain order according to their names it's not sure which function is called first if they have the same name if I understand it correctly (a bug in Unity at the moment?) ?
     
  4. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    It is not a bug. Works like that in most game engines. Its simply non defined. Which in term means that you should not make code that rely on a specific ordering.

    Think we might be talking past each other. Now im not entirly sure what functions you are refering to here:

     
  5. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Maybe this will help.

    Imagine you have 3 game objects (A, B and C) all with the same script attached something like this:

    Code (csharp):
    1.  
    2. function Start() {
    3.    // Do Init
    4. }
    5.  
    6. function Update() {
    7.    // Do update
    8. }
    9.  
    Now when the engine run the game loop, which is run each frame. It will call Start() in a non defined order on the 3 game objects. Once that is done it will the call Update() in a non defined order on the objects.

    Now the order which is garantueed by the engine is that all Start functions will be called before any Update functions are called. Meaning that A.Start(), B.Start() and C.Start is called before it starts calling A.Update(), B.Update() and C.Update().

    It is not garantueed that the engine will always call A.Start() followed by B.Start() then C.Start() it might be different each time.

    Does that make any sense?
     
  6. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    I just think ir's me who didn't explain it good enough - sorry. I just used to the fact that it's possible to set everything in order. But if you make 2 scripts with functions in each with the same name (for instance "start") then the engine doesn't know which one to choose first. Hence a random choice.

    But you can, of course, get around this "issue". Just keep good control of your names of the funtions and keep the naming of certain funtions to a minimum.
     
  7. DGuy

    DGuy

    Joined:
    Feb 7, 2007
    Posts:
    187
    If you need things to happen in a certain order and the Update/LateUpdate events are not enough, have one "Master" update function call the other update functions in the desired order.

    Example:

    You have object A, B and C. These three objects use the same script.

    Instead of naming the update function "Update", which would cause the update functions to get called in some undefined order by Unity, name the update function something like "DoUpdate". Then in your "Master" update function, which is named "Update" and thus will get called by Unity each update-cycle, you call the "DoUpdate" functions in the desired order.

    Code (csharp):
    1. function Update() // this is your 'Master" update function
    2.  {
    3.  A.DoUpdate();
    4.  B.DoUpdate();
    5.  C.DoUpdate();
    6.  }