can someone please explain the difference between an object being active in the scene and active in the hierarchy and how those two states relate to each other?
i saw a video about it but i could use a more clear explanation.
can someone please explain the difference between an object being active in the scene and active in the hierarchy and how those two states relate to each other?
i saw a video about it but i could use a more clear explanation.
If an object is disabled all children are also disabled, even though they may be set as Active themselves.
activeInHierachy returns true if the object is active and also its parents.
The manual explains the differences pretty well I thought : Unity - Scripting API: GameObject.activeInHierarchy
Sorry but activeInHierarchy on the child object will return true if the child object is active, even if its parent is not active.
Edit: wrong code, see posts below for the right code.
Really? The manual is pretty clear.
And
If that’s not how it works then it sounds like a bug to me.
I’m not seeing anything on the issue tracker maybe it’s worth it to submit a bug? At the very least the manual is completely wrong.
How is it logical to have two properties do the exact same thing?
or how is it logical for the manual to spout a bunch of nonsense then?
What is logical is that two properties that do the same thing both return true in the same conditions.
Forget about the documentation.
Are you gaslighting me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class activeCheck : MonoBehaviour
{
[SerializeField]
GameObject child;
void Start()
{
Debug.Log("activeSelf is : " + child.activeSelf);
Debug.Log("activeInHierarchy is : " + child.activeInHierarchy);
}
}
Why would I? I reproduced your code and it works the same for me. There must be something in my code that gives different results.
It was my code, and it makes sense, after more coffee.
An active child object will be inactive if its parent is deactivated, even though it’s not actually deactivated in the hierarchy. Because its parent not being visible on the scene, the child object will also not be visible; consequently it will be considered not active in the hierarchy.
Maybe they need to call this state something else. ![]()
New code:
using UnityEngine;
public class ObjectState : MonoBehaviour
{
[SerializeField] GameObject motherCube;
[SerializeField] GameObject[] childrenGameObjects;
private void Start()
{
motherCube.SetActive(false);
for(int i = 0; i < childrenGameObjects.Length; i++)
{
Debug.Log("Child gameobject " + i + " activeSelf state is " + childrenGameObjects[i].activeSelf);
Debug.Log("Child gameobject " + i + " activeInHierarchy state is " + childrenGameObjects[i].activeInHierarchy);
}
}
}
An object that is active in the hierarchy can be inactive on the scene if its parent object is inactive.
For anyone watching this:
activeSelf returns true if the gameObject is TICKED
activeInHierarchy returns true ONLY if the parent is also active meaning that the gameObject will actually do function.
Fun fact: In case in gameObject is active but it’s parent isn’t… even the child’s Awake function will no execute until the parent is enabled
Now for the Less Fun facts:
Unity 2019.3.14
this.gameObject.activeInHierarchy && this.enabled;
this.isActiveAndEnabled;
Those two lines might give different result. I don’t now if it is a bug or by design.
I learned it the hard way. Basicaly isActiveAndEnabled will return false when it’s Awake or maybe OnEnable was yet to be called for the first time.
koirat
Because “this” usually would refer to Component/Behaviour, where as “go.Active” and "go.activeInHierarchy " refers to gameobjects. Behaviours are components of gameobjects.
To my understanding isActiveAndEnabled refers to “go” and component at the same time.
Generally we Activate GameObjects and Enable MonoBehaviours.