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!
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.
Use GameObject. activeInHierarchy if you want to check if the GameObject is actually treated as active in the Scene.
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:
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:
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 NOTisActiveAndEnabled
?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With