isActiveAndEnabled
and enabled
seems very confusing for beginners, and I'm sure most people still don't know the difference and when to use them. I was one of these people.
Two things to understand:
A.GameObjects can be activated and deactivated.
B. Scripts can be turned on and off.
The keyword in isActiveAndEnabled
should explain all this.
What happens with each property:
1 . For Behaviour.enabled
this is a 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 GameObject
Script is connected to activated or deactivated for Behaviour.enabled
to return true. The important thing is that the Script or component attached to the GameObject is enabled or disabled.
2 . For Behaviour.isActiveAndEnabled
this is a 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
enabled or disabled for Behaviour.isActiveAndEnabled
to return true or false. In order for Behaviour.isActiveAndEnabled
return true
, both components of the GameObject that is connected to script / must be active, and the Script parameter must be enabled. If any of this value is false, then Behaviour.isActiveAndEnabled
will return false
.
EDIT
When do you want to get enabled
and NOT isActiveAndEnabled
?
You use enabled
to check if / w 370> is enabled
. You can also use it to enable or disable the script. Depending on your game logic, there are times when you disable / activate a script. For example, if a GameObject is no longer displayed on the screen, but one Script is attached to it, which performs heavy calculations in the Update()
function, you can disable this Script with it turned on and turn it on later when GameObject is displayed.
isActiveAndEnabled
is read-only. You can use it only to check for Script enabled
and GameObject attached to active
. You cannot use it to enable or activate 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, there is all the information you need to check the status.
Not really. These are very different variables that you will definitely need when using Unity. I don’t think you understand what GameObject.activeInHierarchy
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 :
In the table below, pObj = Parent GameObject and cObj = Child GameObject and that GameObject.activeInHierarchy is executed in 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
almost like isActiveAndEnabled
. It depends on two factors: true
. Parent GameObject must be active. The GameObject (Child) in which the check is performed must also be active. If this value is incorrect, then GameObject.activeInHierarchy
will return false
.
You use GameObject.activeInHierarchy
to check if the provided GameObject is active, and at the same time to check if all of its parents are active. If GameObject does not have a parent, just use GameObject.activeSelf
. GameObject.activeSelf
will check if GameObject is active or not. It will not validate the parent as GameObject.activeInHierarchy
.