Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. We are looking for feedback on the naming of a new user research platform that we are working on.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Order of void Update calling

Discussion in 'Scripting' started by sampa93, Sep 17, 2020.

  1. sampa93

    sampa93

    Joined:
    Jul 18, 2020
    Posts:
    10
    Hello community,

    I am new to unity and got an question to the common "void Update()" method.

    If I got a class which calls in it's update() another class, which also does calculation in it's update() - in which order will it be called? I know update is called once per frame but anyway there must be an order for the classes?

    For example: Will the result be 0 or 1 after the first frame?

    Code (CSharp):
    1. public Class A : MonoBehaviour {
    2.     public ClassB classB;
    3.  
    4.     void Start()
    5.     {
    6.         classB = GetComponent<ClassB>();
    7.     }
    8.  
    9.     void Update()
    10.     {
    11.         int result = classB.Value;
    12.     }
    13. }
    14.  
    15.  
    16. public Class B : MonoBehaviour {
    17.     public int Value = 0;
    18.  
    19.     void Update()
    20.     {
    21.         this.Value++;
    22.     }
    23. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    9,770
    sampa93 likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    4,265
    There is no guaranteed ordering, unless you explicitly create such an ordering using this: https://docs.unity3d.com/Manual/class-MonoManager.html

    However I would recommend instead just writing your code in such a way that it works no matter what order the components are updated in. Perhaps move some of the logic from one of the Update methods into a different public method that the other Update method calls.
     
    sampa93, Dextozz and Kurt-Dekker like this.
  4. OliverTheLove

    OliverTheLove

    Joined:
    Jun 26, 2016
    Posts:
    18
    There also exists [DefaultExecutionOrder(NUMBER)] that you can put over a class to set its order, without going into the editor in the link that PraetorBlue mentioned. They won't however show up in that list either, so beware!
     
    sampa93 likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    9,770
    Thanks, didn't know that. I think it's still better to link the calls explicitly otherwise if you drop code from one project to another, the execution order numbers might not be meaningful. If you explicitly link codepath you will get a compiler error and know you need to fix it.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,541
    Another option is have a single class that has update in it, such as a manager class and have it call a method on other scripts to run an "update".

    Something along these lines. This way you control the update flow yourself and you now only have one script running Update.

    Code (CSharp):
    1. public Script1 script1;
    2. public Script2 script2;
    3.  
    4. void Update()
    5. {
    6.    script1.UpdateFromManager();
    7.    script2.UpdateFromManager();
    8. }
     
    Kurt-Dekker likes this.
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,238
    In practice the order of Updates rarely matters. If you think A runs before B but B actually goes first, your count is off by 1, in theory. But it's not like you need an exact count most times. You probably playtest and decide "about 125" is when the cows should appear, and so on. It's also reliable. Unity keeps the Update order, even on a different platform (I think). Or suppose A dies and you expect B to notice on the same frame, but since B runs first it takes an extra frame. No problem. That may look better and you were probably going to add a random 10-15 frame delay anyway.

    The order of initialization often does matter -- if B reads values from an array created in A::Start(), A clearly needs to run first. Unity suggests you use Awake and Start for those (all Awakes run, then all Starts).
     
    sampa93 likes this.
unityunity