Difference between enabled, isActiveAndEnabled and activeInHierarchy in Unity

In addition to other answers, IsActiveAndEnabled seems to check whether GameObject is active in its hierarchy (like IsActiveInHierarchy), not just if that GameObject is active (like activeSelf). This is not written in Unity documentation. Below is a truth table for all states.

+-------------------+-----------------------+------------------------------+------------------------------+------------------------------+
| Component.enabled | GameObject.activeSelf | Parent GameObject.activeSelf | GameObject ActiveInHierarchy | Component isActiveAndEnabled |
+-------------------+-----------------------+------------------------------+------------------------------+------------------------------+
| TRUE              | TRUE                  | TRUE                         | TRUE                         | TRUE                         |
| FALSE             | TRUE                  | TRUE                         | TRUE                         | FALSE                        |
| TRUE              | FALSE                 | TRUE                         | FALSE                        | FALSE                        |
| FALSE             | FALSE                 | TRUE                         | FALSE                        | FALSE                        |
| TRUE              | TRUE                  | FALSE                        | FALSE                        | FALSE                        |
| FALSE             | TRUE                  | FALSE                        | FALSE                        | FALSE                        |
| TRUE              | FALSE                 | FALSE                        | FALSE                        | FALSE                        |
| FALSE             | FALSE                 | FALSE                        | FALSE                        | FALSE                        |
+-------------------+-----------------------+------------------------------+------------------------------+------------------------------+

isActiveAndEnabled and enabled seems very confusing to beginners and I bet most people still don't know the difference and when to use each one. I was one of these people.

Two things to understand:

A.GameObjects can be activated and de-activated.

B.Scripts can be enabled and disabled.

The keyword in isActiveAndEnabled should explains it all.

What happens with each property:

1.For Behaviour.enabled, this is the truth table:

  • GameObject = Active AND Script = Enabled then Behaviour.enabled = true.
  • GameObject = Active AND Script = Disabled then Behaviour.enabled = false.
  • GameObject = Inactive AND Script = Enabled then Behaviour.enabled = true.
  • GameObject = Inactive AND Script = Disabled then Behaviour.enabled = false.

It doesn't matter if the GameObject the script is attached to is activated or deactivated for Behaviour.enabled to return true. What matters is if the script or component that is attached to the GameObject is enabled or disabled.

2.For Behaviour.isActiveAndEnabled, this is the truth table:

  • GameObject = Active AND Script = Enabled then isActiveAndEnabled = true.
  • GameObject = Active AND Script = Disabled then isActiveAndEnabled = false.
  • GameObject = Inactive AND Script = Enabled then isActiveAndEnabled = false.
  • GameObject = Inactive AND Script = Disabled then isActiveAndEnabled = false.

It matters if GameObject is enabled or disabled for Behaviour.isActiveAndEnabled to return true or false. In order for Behaviour.isActiveAndEnabled to return true, both the GameObject the script/component is attached to must be active and the script must be enabled. If any of this is false, then Behaviour.isActiveAndEnabled will return false.

EDIT:

When would you want to get enabled and NOT isActiveAndEnabled?

You use enabled to check if a script is enable/disabled. You can also use it to enable or disable a script. Depending on your Game logic, there are times when you disable/enable a script. For example when a GameObject is no longer visible on the screen but you have one script attached to it that is doing heavy calculation in the Update() function, you can disable that script with enabled and enable it back later on when the GameObject is visible.

isActiveAndEnabled is read only. You can only use it to check if both the script is enabled and the GameObject it is attached to active. You cannot use it to enable or activate the GameObject.

if (myScriptInstance.isActiveAndEnabled) is a short hand for if (myScriptInstance.gameObject.activeSelf && myScriptInstance.enabled) but isActiveAndEnabled makes your code shorter and easier to read.

Between GameObject.activeInHierarchy and Behaviour.enabled, one has all the information one needs to check state.

Not really. These are very different variables you will definitely need when using Unity. I don't think you understand what GameObject.activeInHierarchy is used for. To check if GameObject is active or not, use GameObject.activeSelf not GameObject.activeInHierarchy. To activate/deactivate GameObject use GameObject.SetActive(true/false);

3.GameObject.activeInHierarchy and when to use it:

For the table below, pObj = Parent GameObject and cObj = Child GameObject and that GameObject.activeInHierarchy is being performed on the Child GameObject.

  • pObj = Active AND cObj = Active then GameObject.activeInHierarchy = true.
  • pObj = Active AND cObj = Inactive then GameObject.activeInHierarchy = false.
  • pObj = Inactive AND cObj = Active then GameObject.activeInHierarchy = false.
  • pObj = Inactive AND cObj = Inactive then `GameObject.activeInHierarchy = false.

GameObject.activeInHierarchy is almost like isActiveAndEnabled. It depends on two things for it to be true. The parent GameObject must be active. The GameObject(Child) the check is being performed on must also be active. If any of this is false, then GameObject.activeInHierarchy will return false.

You use GameObject.activeInHierarchy to check if the provided GameObject is active and the-same time to check if all of its parents are active too. If the GameObject does not have parent then simply use GameObject.activeSelf. GameObject.activeSelf will only check if the GameObject is active or not. It won't check the parent like GameObject.activeInHierarchy.


I know it is misleading:

GameObject.activeSelf = Is the game object active regardless of its parents

We expect a game object to be active when its whole hierarchy is active. But activeSelf tells us whether or not that particular game object is active, and ignores the active status of the parents.

GameObject.activeInHierarchy = Is the game object (and all its parents) actually active and therefore updating


Behaviour.isActiveAndEnabled = Has the Behaviour had enabled called.

In order for a behaviour to has enabled called, its game object (and all its parents) must be active AND the behaviour itself must be enabled.

Behaviour.enabled = Enabled Behaviours are Updated

In order for a behaviour to be updated, the checkbox must be checked. however, if the game object (or one of its parents) isn't active then it cannot be updated so its attached behaviours cannot be updated.


So in short:


Behaviour.isActiveAndEnabled: component is enabled & game object hierarchy is active

Behaviour.enabled: component is enabled & game object hierarchy may be active or deactivated


GameObject.activeSelf: game object is active, its parents may not

GameObject.activeInHierarchy: game object and all its parents are active


Tags:

C#

Unity3D

Unity5