Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Scripts Execution Order Not Working?

Discussion in 'Scripting' started by III111III, Aug 17, 2023.

  1. III111III

    III111III

    Joined:
    Nov 8, 2017
    Posts:
    12
    Hi,
    When I click the + icon of the Scripts Execution Order, I only see the DOTween scripts and I can't choose one of my own scripts.
    Drag and drop doesn't work either.
    I use Unity 2022.3
     

    Attached Files:

  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    To be fair, you own script execution order shouldn't be handled internally. That should be handled within your code structure.

    Now personally I use Inheritance for all my scripts, and I haven't noticed any order of my Awake() methods deferring from the order of Inheritance. I could be wrong on that, I'm just saying I haven't noticed any problems.

    But if I were to be completely strict on how each class should "boot up", then the gameObjects containing the scripts would be disabled in the scene hierarchy, and when the "Master" script was done thinking, it would then wake up the next order of gameObject containing the next script to wake up. If I chose to do it that way..

    For the most part, if you're trying to do "too much" within Awake() methods, it is best to just have them do all they can, as precursors to "first" logic. Then have the Start() method(which comes after all Awakes have run), do the "second" part of logic.

    Unless you have a particular issue, where you might need a "third" order of logic? The times I needed that as well, was in a one time call at the beginning of the Update() method, to run on the scripts first frame.

    I'm not sure if I helped answer your particular issue, but I took a shot in the dark for ya :)
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Does this happen in a fresh project of the same version?

    Just looks like a bug to me.

    Though script execution order should be a last-resort tool.
     
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    I don't want to be mean, but this does not solve his problem at all, plus from my perspective this method is very error prone, sometimes not possible to implement, and has several other minor problems. On top o that there are various situations you just need execution order.

    I don't know how to solve the dropdown problem, but be aware there is also attribute
    [DefaultExecutionOrder()]
    I heavily use.
     
    spiney199 likes this.
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    This very well possible, as I haven't had any issues, and all Debug.Log/benchmark printouts show me exactly how I hoped it to execute. So it is possible there could be some sort of variance I haven't encountered yet. Thanks for the heads up. :)
     
  6. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    The thing is you can't trust something that is not documented, because it might suddenly change in the future.

    For example when you close the scene OnDisable and OnDestroy is called and there is most likely hierarchical rule (order of called OnDestroy), but the root gameobjects are not called in the order you see in the inspector, one time it might call first, second and third root object, but next time you load unity it might destroy third root object as the first one.

    The same thing with execution order of components in the inspector (on one object). One might think they are called from top to down, but this is not true, they are initialy called from the bottom, but (probably) only becuase the memory layout you made by adding them, and if you change order (move up/down) or load unity again they might be called in completely random way.
     
    wideeyenow_unity likes this.
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    I've never encountered a situation where order of execution was an issue. We have Awake/Start/OnEnable for initialisation, and OnDisable/OnDestroy for de-initialisation. There's plenty of room to avoid race conditions.

    If I ever needed a particularly explicit order of things, then some top level manager that is open to subscriptions does the job.

    Eg: In my puzzle platformer, I have three global events that anything can subscribe to during scene load to have a very explicit scene initialisation. And it really just comes down to this:
    Code (CSharp):
    1. public static void InitiateLevel()
    2. {
    3.    Debug.Log($"Level Manager: Calling {nameof(OnEarlyInitialiseLevel)}");
    4.    LizardGlobalEvents.RaiseEvent<OnEarlyInitialiseLevel>();
    5.  
    6.    Debug.Log($"Level Manager: Calling {nameof(OnInitialiseLevel)}");
    7.    LizardGlobalEvents.RaiseEvent<OnInitialiseLevel>();
    8.  
    9.    Debug.Log($"Level Manager: Calling {nameof(OnBeginLevel)}");
    10.    LizardGlobalEvents.RaiseEvent<OnBeginLevel>();
    11. }
    12.  
     
    wideeyenow_unity likes this.