OnEnable before Awake??

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.

2 Likes

Awake → OnEnable → Start

scroll down, nice chart :slight_smile:

5 Likes

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:

Obj1.Awake();
Obj2.Awake();
Obj1.OnEnable();
Obj2.OnEnable();
Obj1.Start();
Obj2.Start();

As you’ve noticed, this is not the case.

This is an example of what it actually does (actually vaguely explained on the page @martinmr linked)

Obj1.Awake();
Obj1.OnEnable();
Obj2.Awake();
Obj2.OnEnable();
Obj1.Start();
Obj2.Start();

So OnEnable is called just after Awake per object.
One object’s Awake is not guaranteed to run before another object’s OnEnable.

59 Likes

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).

You can see it in here:

4 Likes

ah ok if you want to know it between different scripts you have to also have to think about a Script Execution Order

so like documentation says, let’s look what wood happen if we have 3 Scipts.

Script_1 with Awake() OnEnable() and Start (), the same with Script_2, Script_3 .

If all of our Script are on default time because we haven’t done any adjustments in the ScriptExecution scripts will be called like:

Script_1.Awake()
Script_1.OnEnable()
Script_2.Awake()
Script_2.OnEnable()
Script_3.Awake()
Script_3.OnEnable()

Script_1.Start()
Script_2.Start()
Script_3.Start()

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

Script_3.Awake()
Script_3.OnEnable()
Script_2.Awake()
Script_2.OnEnable()
Script_1.Awake()
Script_1.OnEnable()

Script_3.Start()
Script_2.Start()
Script_1.Start()

Hope this makes all clear, if still a question, just ask here :smile:

10 Likes

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).

    public abstract class BaseFramework : MonoBehaviour
    {
        protected bool bInitialised = false;

        // MonoBehaviours
        protected virtual void Awake() { Initialise(); }
        protected virtual void OnEnable() { if (!bInitialised) return; }

        // protected methods
        protected virtual bool Initialise()
        {
            // Other initialisation stuff
            return (bInitialised = true);
        }
    }

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 :slight_smile:

In your case I would probably set a bool to true in start and then check it in OnEnable, and do nothing if start has not run yet.

1 Like

OnEnable can be a coroutine. It’s possible to yield a frame or two before taking any action.

2 Likes

but that would mean the first OnEnable might not get run for a very long time if the Object is not disabled and enabled again.

yeah, basically what I did, but since I didn´t need to pass anything, I just used Invoke with a small delay.