Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Active Self VS Active in Hierarchy?

Discussion in 'Editor & General Support' started by Kibllex, Jan 11, 2020.

  1. Kibllex

    Kibllex

    Joined:
    Jul 14, 2019
    Posts:
    8
    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.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,065
    sparkwd, DhiaSendi, steril and 4 others like this.
  3. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    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.
     
    Last edited: Jan 11, 2020
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,065
    Really? The manual is pretty clear.
    And
    If that’s not how it works then it sounds like a bug to me.
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,065
    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.
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,065
    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?
     
  7. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    What is logical is that two properties that do the same thing both return true in the same conditions.

    Forget about the documentation.
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,065
    Are you gaslighting me?

    activeHierarchy.png
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class activeCheck : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     GameObject child;
    10.  
    11.     void Start()
    12.     {
    13.         Debug.Log("activeSelf is : " + child.activeSelf);
    14.         Debug.Log("activeInHierarchy is : " + child.activeInHierarchy);
    15.     }
    16. }
     
    ImFromTheFuture, Fitbie, RHHH and 2 others like this.
  9. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    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.
     
  10. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    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:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ObjectState : MonoBehaviour
    4. {
    5.     [SerializeField] GameObject motherCube;
    6.     [SerializeField] GameObject[] childrenGameObjects;
    7.  
    8.     private void Start()
    9.     {
    10.         motherCube.SetActive(false);
    11.  
    12.         for(int i = 0; i < childrenGameObjects.Length; i++)
    13.         {
    14.             Debug.Log("Child gameobject " + i + " activeSelf state is " + childrenGameObjects[i].activeSelf);
    15.             Debug.Log("Child gameobject " + i + " activeInHierarchy state is " + childrenGameObjects[i].activeInHierarchy);
    16.         }
    17.     }
    18. }
    Sans-titre-1.jpg
     
    Last edited: Jan 11, 2020
  11. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    An object that is active in the hierarchy can be inactive on the scene if its parent object is inactive.
     
  12. A_Savvidis

    A_Savvidis

    Joined:
    Jul 21, 2016
    Posts:
    97
    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
     
    furkan83, naknuknik, Matsyir and 9 others like this.
  13. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,010
    Now for the Less Fun facts:
    Unity 2019.3.14
    Code (csharp):
    1.  
    2. this.gameObject.activeInHierarchy && this.enabled;
    3. this.isActiveAndEnabled;
    4.  
    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.
     
  14. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    koirat
    Because "this" usually would refer to Component/Behaviour, where as "go.Active" and "go.activeInHierarchy " refers to gameobjects. Behaviours are components of gameobjects.
     
    MimiKitty likes this.
  15. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,010
    To my understanding isActiveAndEnabled refers to "go" and component at the same time.
    Generally we Activate GameObjects and Enable MonoBehaviours.