Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between enabled, isActiveAndEnabled and activeInHierarchy in Unity

Tags:

c#

unity3d

unity5

I cannot believe that this question has not already been asked somewhere; a fairly thorough Googling has turned up no results. The Unity documentation says this about the Behaviour.isActiveAndEnabled field:

Has the Behaviour had enabled called.
True while the behaviour is enabled, false when disabled.

And it says this about Behaviour.enabled:

Enabled Behaviours are Updated, disabled Behaviours are not.
This is shown as the small checkbox in the inspector of the behaviour.

So one article just elaborates on what "Enabled" means more than the other. As far as I can tell, these fields are true/false under the exact same conditions! So what is the difference? When would you use one over the other? A clarification would be much appreciated!

like image 856
Rabadash8820 Avatar asked Nov 20 '16 18:11

Rabadash8820


People also ask

What is activeInHierarchy?

Defines whether the GameObject is active in the Scene. This lets you know whether a GameObject is active in the game. That is the case if its GameObject.

How to check GameObject is active or not?

Use GameObject. activeInHierarchy if you want to check if the GameObject is actually treated as active in the Scene.


2 Answers

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.

like image 158
Programmer Avatar answered Sep 27 '22 21:09

Programmer


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


like image 44
Bizhan Avatar answered Sep 27 '22 21:09

Bizhan