Difference between included, isActiveAndEnabled and activeInHierarchy in Unity

I cannot believe that this question has not yet been asked; a rather thorough googling did not get any results. Unity documentation says this to Behaviour.isActiveAndEnabled :

Whether Behring behavior is allowed.
True when the behavior is enabled, false when disabled.

And he says this to Behaviour.enabled :

Enabled Behavior updated, disabled Behavior - no.
This appears as a small flag in the behavior inspector.

So, one article just clarifies that “Enabled” means more than another. As far as I can tell, these fields are true / false under the same conditions! So what is the difference? When will you use one over the other? An explanation would be very helpful!

+6
source share
3 answers

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 .

+8
source

In fact, I also can not believe it!

Behaviour.isActive And Enabled = Whether the behavior is enabled.

In order for the called behavior to be activated, its game object must be active, and the behavior itself must be enabled.

Behaviour.enabled = Enabled Behavior updated

To update the behavior, check the box. however, if the game object is not active, it cannot be updated, so its attached actions cannot be updated.

So

The Behaviour.isActiveAndEnabled: component is enabled and the game object is active

The Behaviour.enabled: component is enabled , and the game object does not matter

+1
source

I noticed that isActiveAndEnabled is false when the Awake event was not raised.

0
source

Source: https://habr.com/ru/post/1012510/


All Articles