Search Unity

Feedback Scripts execution order problem

Discussion in 'Scripting' started by Sergey_Suslov, Sep 11, 2019.

  1. Sergey_Suslov

    Sergey_Suslov

    Joined:
    Nov 20, 2017
    Posts:
    12
    Hello there!
    I would like to submit an improvement idea for unity from our team.

    MonoBehaviours with the same execution time are executing in unobvious, maybe random order. The order can be different on different devices or editors.
    Sometimes this cause floating bugs which can be very hard to reproduce and locate.

    Why not sort execution order by guid from scripts metafiles? This solution would not be affected by script renaming, moving, or scene hierarchy. Even in case of some execution order related bugs, they will be easily reproduced and fixed.

    Thanks!
     
    harjotmadhopuri and knispeja like this.
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    You can actually set the execution order of specific scripts.

    https://docs.unity3d.com/Manual/class-MonoManager.html

    But even then, having bugs from an execution order failure sounds more like you have a fail in your design.
    If you really need things to execute in order for lets say initialization, you could make a boot scene which loads the things you need and then additionally load the scene with the scripts that require the initial initialization.
     
    Bunny83 likes this.
  3. Sergey_Suslov

    Sergey_Suslov

    Joined:
    Nov 20, 2017
    Posts:
    12
    Yes, it's in-project failture. In large projects, it's kinda difficult to do every little piece right from the first attempt. Fixed execution order will make these errors just easier to find.
     
    Last edited: Sep 11, 2019
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Don't write any code that assumes execution order, or use manual execution order.

    We used to use execution order back in the days, and oh boy did a lot of bugs go away when we just stopped doing that, and made sure self-initialization happens in Awake, and initialization based on other things happen in Start. If that's not doable, use lazy properties.

    The execution order selection should probably go away, as it encourages bad practices. It (probably) also slows down things.
     
  5. Sergey_Suslov

    Sergey_Suslov

    Joined:
    Nov 20, 2017
    Posts:
    12
    It's easy to say "just write everything perfectly and you will not have a problem", but in large projects with deadlines and different people working, there is no possibility of doing every single thing right from the first attempt.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    True, but this isn't one of those cases.

    You only really care about execution order when you get bugs due to script A trying to access script B, but script B isn't ready yet. Now there's two ways you can fix this:
    1: Set B to have an earlier execution order than A
    2: Move the initialization of the things A need to B.Awake, and move A's access out of A.Awake

    Both take about as long to do, but 2 is easier to reason about an less buggy. So it's not about "perfect" vs. "deadlines", it's just about doing the correct thing to begin with, at zero cost.

    You'll probably save a lot of time by clearing the execution order settings list, and handling the fallout right away.
     
  7. Sergey_Suslov

    Sergey_Suslov

    Joined:
    Nov 20, 2017
    Posts:
    12
    We almost not use execution order list. Most of our code is execution-order safe. The exact issue is when someone forgot to make it ex-order safe, and everything is working on his device, but do not work on another.
     
  8. TheHeftyCoder

    TheHeftyCoder

    Joined:
    Oct 29, 2016
    Posts:
    91
    Just wanted to comment on this year old thread. Execution order is a must have feature and should be implemented in script and not by the editor.

    Having things happen in Awake and Start, while it sounds right at first, does not always help. Someone might as well want to instantiate a prefab that is composed of a complicated setup of components. If he wanted to use that prefab upon instantiation, many things could probably break, since a frame must be skipped in order for all the Start functions to execute.