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 Script Execution Order settings seem to be ignored

Discussion in 'Scripting' started by SpiderJones, Aug 23, 2023.

  1. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    I looked at this -> https://docs.unity3d.com/Manual/class-MonoManager.html

    I've added a script and set it so it executes before the "default" time (gave it a negative number), but other MonoBehaviors in the scene execute before this script. Their Awake methods are called before the script I set to execute before the "default time".

    Any tips?
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    The execution order only applies to "batches" of script being initialized. That's when a scene is loaded or when something is instantiated. So if you e.g. load multiple scenes, the order only applies to each individual scene, or when you instantiate a prefab while loading, the prefab's OnEnable/Awake will always be called immediately on instantiation. Some scripts might also use the undocumented DefaultExecutionOrder attribute, in which case I think they don't appear in the project settings. Also make sure you apply the changes and wait for the recompile/reload to complete, this is required for the order to apply.
     
    Last edited: Aug 23, 2023
    Bunny83 and Yoreki like this.
  3. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    So if I set a script to execute before the "default time", shouldn't its Awake method be called before the other MonoBehavior classes in my scene?
     
  4. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I've given up on this so I created an event on GameController that I named ReadyToGo, then the GameController waits until the event is fired to continue initialization.
     
  5. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    Yes. They are both in the same scene and neither of them is being instantiated? And you've put a
    Debug.Log
    in both
    Awake
    methods to check the order?
     
  6. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    Both scripts are in the same scene. I put a Debug.Log in each class's Awake. In the editor, the script set to execute before "Default Time" does execute first, but only when I place it on a separate GameObject. When I test on device (Android) the script set to execute before "Default Time" does not execute first.
     
  7. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    Assuming your setup is correct it should not happen. Just to be sure:
    1. Both scripts are already on the scene (not instantiated)
    2. Both scripts have got negative order (the first one is lowest number: -100, -50, 0, 20, 60)
    3. Both game objects are enabled and active from the very beginning
     
  8. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    1. Both scripts are already on the scene (not instantiated)
    ** That is correct

    2. Both scripts have got negative order (the first one is lowest number: -100, -50, 0, 20, 60)
    ** Only one script is added to Script Execution Order and it's set to -1200.

    3. Both game objects are enabled and active from the very beginning
    ** That is correct

    And the one script that is added to Script Execution Order is having its Awake called after the script that is not added to Script Execution Order when running on Android...
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Have you tried with the undocumented attribute?

    What's your end goal here too?
     
  10. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    I have not tried the undocumented approach.

    My end goal is to have a script that registers services before all other MonoBehaviors execute their Awake methods.
     
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Can you do this outside of the monobehaviour realm? Such as with a scriptable object stored in pre-loaded assets, or with
    [RuntimeInitializeOnLoadMethod]
    ?
     
  12. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    Try it perhaps it will work, but send the bug ticket anyway, I can't see reason why it should not work.
     
  13. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    If no other way is helping with script ordering, the only thing I can think is to Poll() them. Meaning have the script(Awake()?) run it's logic, then tell other scripts to "technically" do their Awake() with a function call. Then the order is simply what script you put first to Poll and so on.
     
  14. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    thank you all. I'll try some of the options you mentioned when I have time. But for now I have the main class in the scene calling an init method in the script that registers services.
     
  15. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    I tried using
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]. And it has the same issue. In the editor it works, but not on Android.
     
  16. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    In case my explanation made no sense:
    Code (CSharp):
    1. public class FirstRunScript
    2. {
    3.     void Awake()
    4.     {
    5.         // my Awake logic
    6.         ScriptOrder1.RunMyAwake();
    7.         ScriptOrder2.RunMyAwake();
    8.         ScriptOrder3.RunMyAwake();
    9.         ScriptOrder4.RunMyAwake();
    10.     }
    11. }
    12.  
    13. public class ScriptOrder1
    14. {
    15.     public void RunMyAwake()
    16.     {
    17.         // my Awake logic
    18.     }
    19. }
    20.  
    21. // etc...
    22.  
     
  17. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,494
    I think your explanation made more sense than your code snippet here ^^. It doesn't really help when you post broken or incomplete examples.
     
    wideeyenow_unity likes this.
  18. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Yeah, wasn't sure.. everything I look for on Polling through google gives a ton of weird results, but it's just what I heard that being called.

    Kind of like how you make a manager script that runs every transform in a List, versus each class having it's own Update and moving it's own transform. I keep hearing how much faster it is, since calling each "Update()" chews up time.. But haven't got around to benchmarking it yet..

    But as far as a particular order, if you command it, it will do. :)
     
  19. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] is working for a static method in a non MonoBehavior class. Works in the editor and on Android. But when it's applied to a non static method it's not working on Android.
     
  20. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    It should only work on static methods. It obviously can't run on a non-static method as how would it locate live instances to call the method on?
     
  21. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    What is your unity version?
    Could you pack your current setup as unity package and share here so we can test it?
    Also screenshot of your execution order settings.
     
  22. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    The Unity version is 2022.3.7f1

    I can not send the project.

    I've attached a screenshot. My script is "RegisterServicesManager"
     

    Attached Files:

  23. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    216
    I've tried a few scripts, all had the same result. Seems to work in Editor Play Mode. Doesn't work on Android. I haven't tried testing with a new and clean Unity project.
     
  24. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    I do not ask for the whole project, but one folder with example scene, but it's fine.