Warm tip: This article is reproduced from stackoverflow.com, please click
c# unity3d unity5

Difference between enabled, isActiveAndEnabled and activeInHierarchy in Unity

发布于 2020-05-27 10:10:49

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!

Questioner
Rabadash8820
Viewed
15
Programmer 2016-11-21 07:16

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.