I always thought it was Awake, Start then OnEnable
Two game objects, each with a script.
on the first object, the script has an awake function that sets some public variables
on the second object (enabled) there is a script that uses those variables in OnEnable
it is a bit curious, because if I start the game with the second object already enabled, the variables I am getting in the second script are 0´s because the variables have not yet been set by the first scripts Awake.
I solved the problem by invoking a method in the OnEnable function .1f seconds later. Just curious of an explanation of why that would be needed.
What the chart doesn’t explain is how executions of this is ordered between objects. One way you could interpret it is as if it will always call all Awakes before all OnEnables:
Yeah, I’m not a fan of this ordering myself either.
I like to have code in OnEnable and OnDisable that turn on or off some stuff in my script based on that. But my OnEnable expects other scripts to be prepared and ready (Awake was called).
At first I was checking if the script had been started and calling the enable code from both Start and OnEnable. But then I encapsulated it into a special base class that all my scripts inherit from that has a new method called ‘OnStartOrEnable’ that calls in the order in the way I would rather (and you seem to expect).
but what at this point is important. if they are all at default time, it is not 100% sure that the scripts methods will be called this way every time. what also could happen is das Script_3.Awake() is called before the other Script_1.Awake() and Script_2.Awake(). So if you build to your phone the internal order of script execution at default time can change and you may get some errors.
But what NOT will happen is, that Script_3 Script_2 or Script_1.Start() will be called before an OnEnable or Awake of any other script!
So if we now set the Script Excecution of Script_1 as 100, Script_2 stays Default Time, Script_3 to -100.
(Every Script not shown in the list ate the ScriptExecution are standard at Default Time).
In this case our order how Start Awake and OnEnable will be called now this way
Yeah OnEnable always gets called multiple times for some reason. It’s why I always have a base class with a bInitialised check, to see whether Awake() or Start() has been called (whichever I wanna check).
Ok, thanks for the explanation. I think my solution will work fine for my situation ( having a delayed invoke inside the OnEnable), but I should probably delay the invoke a little more for the live environment where I will have a lot more scripts with awake. The vast majority of the time, the OnEnable will be getting run after the game has been running so won´t have this issue, but that initial OnEnable on startup could have a bunch of my trees starting the game as chopped stumps