Translate to English, please, OnEnable() "Documentation"

From the documentation:

OnEnable: (only called if the Object is active): This function is called just after the object is enabled. This happens when a MonoBehaviour instance is created, such as when a level is loaded or a GameObject with the script component is instantiated.

This word salad is insane

If an object is active, how is it not already enabled? How could it be both active and in need of being enabled?

(((How do I insert a paragraph space in here)))

Does OnEnable also get called when an object’s enabled state is toggled False then True?

Using the right terminology is vital to understand what the documentation is talking about.

GameObjects (which are just containers) have an active state. This can be controlled with the SetActive method of the gameobject. The active state is organised in a hierachical structure. So the state of the parent is “inherited” by all child objects. A GameObject is only active when itself is set active (can be checked with the activeSelf property) and all of the parents are active as well. If one parent has been deactivated, all children and grand children will be deactivated as well. To check if a certain gameobject is actually active or not one can use the activeInHierarchy property which is only true when the gameobject itself and all it’s parents are active.

Deactivated GameObject behave like they don’t exist at all. They are essentially excluded from almost any processing inside the engine.

Apart from the active state of the gameobject, components which are attached to gameobjects can have an “enabled” state as well. This enabled state only controls the behaviour of this one component. The enabled state of components are also bound to the active state of the gameobject since, as i said above, gameobjects are essentially dead if it’s not active. In other words components which have an enabled state (like MonoBehaviour derived classes and some build-in components) only work when they are enabled and the gameobject it’s attached to is activeInHierarchy.

The various callbacks that a script component (MonoBehaviour derived class) can receive depend on the state change of the enabled property and active state of the gameobject.

OnEnable and OnDisable are called whenever the script is enabled or disabled. This can happen due to various actions. First of all scripts which are attached to a gameobject in a scene get OnEnabled called when the scene loads only if the gameobject is active and the script is enabled. Whenever you disable any parent gameobject the script component will get OnDisable called. This also happens when you set the enabled property to false.

In short: OnEnabled is called the moment the script is actually “running” (when it’s “enabled”) and OnDisable is called as soon as the script is disabled. As far as i remember the OnEnabled / OnDisable callback is actually invoked from inside the enabled property / SetActive call.

Unlike OnEnable / OnDisable which are called everytime the script is enabled / disabled, there is also the Awake and the OnDestroy callback. Those are independent from the enabled state of the script. Those are only bound to the active state of the gameobject. Also both, Awake and OnDestroy are only called once or not at all in the lifetime of a component. Awake is called as soon as the gameobject the script is attached to is active for the first time. So usually an active gameobject in a scene get Awake called right after it has been loaded. If the script is enabled Awake and OnEnabled will be called right after the other. OnDestroy will be called when the script component is destroyed. Of course whenever the parent gameobject is destroyed all components attached to that gameobject will be destroyed as well. I’ll add the same note as the documentation: OnDestroy is only called when the object was active at least once. So only if Awake has been called previously. So a deactivated object that stays deactivated all the time does not get the OnDestroy callback when it’s destroyed since it never was actually an active part of the scene.

So the important thing to remember is the actual logical hierarchy of the active state of the gameobject(s) and the enabled state of attached components. Once you understand this relationship the purpose of the callbacks should become clear.

Just to directly answering your sub question: A script that is disabled (enabled set to false) but is attached to a gameobject that is active does not get OnEnable called since the script is not enabled yet. Just in theory you could now deactivate the gameobject (SetActive(false)) and set enabled to true. This will not call OnEnable since the gameobject is not active.

ps: about the paragraphs here on UA: About 2 years ago there was a (strange) update on UnityAnswers which broke the Markdown renderer. paragraphs, bulletpoint lists and some other features are no longer interpreted correctly. Unfortunately there’s nothing we can do about that. Currently to insert a paragraph you have to use an empty line followed by followed by another empty line.

Hello there,

An object can be enabled, but inactive. For example, if the parent of that object is disabled.

Let’s say you have your object (A) with a script that contains an OnEnable() function.

• A doesn’t have a parent, and is enabled: OnEnable() is called.

• A doesn’t have a parent, and is disabled: OnEnable() is NOT called.

Now imagine that A is a child of another object, B.

• B is enabled, A is enabled: OnEnable() is called.

• B is enabled, A is disabled: OnEnable() is NOT called.

• B is disabled, A is enabled: OnEnable() is NOT called.

• B is disabled, A is disabled: OnEnable() is NOT called.

It might be strangely worded, but it is actually correct.

Also, on Unity Answers you need to add a linebreak + <•br•> + linebreak to create a space between lines (remove the black dots on the <•br•> tag).

And yes, toggling an object on and off repeatedly will trigger its OnEnable() multiple times, assuming its parent itself is active.

Hope that helps!