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!
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
.
Thanks for the thorough answer! That's pretty much how I figured it worked, I just don't understand why Unity has this field. Between
GameObject.activeInHierarchy
andBehaviour.enabled
, one has all the information one needs to check state. I guessisActiveAndEnabled
is shorter/cleaner, but what they gained in syntactic sugar I feel like they lost in API clarity...I guess my question now is, when would you want to get
enabled
and NOTisActiveAndEnabled
? Seems likeenabled
should only be used for setting, not getting.Before I answer that, I have a question for you. Do you know the difference between GameObject and Script/Component?
Lol yes. Don't know how I would convince u of that, but I've written several thousand lines of C# code in managed plugins so yes I'm pretty hip to the difference :P you attach Components to GameObjects
Lol I feel horrible for asking that but I just wanted to make sure you are not new to Unity and understood both. I will edit my answer to answer that.