Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

game object's activeSelf == true while activeInHierarchy == false, but no parent

Discussion in 'Scripting' started by Deleted User, Oct 31, 2016.

  1. Deleted User

    Deleted User

    Guest

    Under what circumstances is this possible please? A coroutine refuses to run (sometimes) because the console claims my game object is inactive. Debug.Log says activeSelf == true and activeInHierarchy == false, though the object has no parent. It is however an instance of a prefab, which itself has no parent and has the 'active' box checked in the Inspector. Thanks in advance
     
    Arlorean, EZaca and Xriuk like this.
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,992
    The usual thing to do is to try to isolate the problem. Forget the coroutine. You have a seemingly impossible situation - a parentless object both active and inactive. Try to pare it down to the smallest example causing the problem. Maybe you'll figure out your test lines are wrong, or you made some obvious mistake. Or maybe you'll find that A then B then C causes a real bug - submit a report and try to work around it.
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I can't really think of a situation where a GameObject could be activeSelf == true but show up as activeInHierarchy == false without a parent existing simultaneously in the same function. You can definitely, however, have that happen in the same frame, if doing one of the checks in one component while doing the second check in another, with something disabling the GameObject in between.

    I would narrow things down to only the components that actually have "disabling" power over your GameObject and then make sure that you aren't making assumptions about the script execution order that only sometimes proves to be true- there's no guarantee that A component's Update function (for instance) will run before B component's Update function, unless it's specifically set in the Script Execution Order list.
     
    Bunny83 and (deleted member) like this.
  4. Deleted User

    Deleted User

    Guest


    Ok thanks, that gives me something to work with. Posted on Reddit also but no satisfactory responses. Thanks again will implement tomorrow
     
  5. Deleted User

    Deleted User

    Guest


    Thank you!! Hard to get real answers out there, will implement tomorrows
     
  6. Deleted User

    Deleted User

    Guest

    http://imgur.com/a/1AId5

    Here's my console after commenting out the coroutine; gameplay apparently works fine except for the events in the coroutine. The player object in question is always in view during the game. I am stumped at this point.
     
  7. Deleted User

    Deleted User

    Guest

    I moved a bit of code around - code which includes a reference to my game object's Rigidbody - and now when activeInHierarchy becomes false (coroutine disabled, btw) the reference to the Rigidbody gives a NullReferenceException - this is consistent with the activeInHierarchy == false problem... the only disabling in any of my code is on GUI items...
     
  8. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    There is a bug in GameObject.SetActive(), i am convinced now. But it's random in nature, as in it works in 99% of the cases, but then some random case it leaves a disabled thing still active. Just now i've got to deal with this problem where it works in Unity UI and on my android phone to hide a UI frame, but on someone elses phone it leaves active on the background.

    For a workaround you can make a coroutine that will do the SetActive a second time. It might fix it for you.

    Did you check if the parent is actually null, or some other value?
     
    Last edited: Nov 1, 2016
  9. Deleted User

    Deleted User

    Guest

    Trying to narrow it down and here's the essence of the problem: I have a function in one of the scripts on my player. If the function is empty of code my game works. If there is are two Debug.Log lines in the function, and only those, the player objects activeInHierarchy bool often goes to false and I get errors saying my player is inactive. Here's my console when the function has 2 Debug.Logs:

    http://imgur.com/a/cF6fu

    and the code snippet:

    public void OnIppon()
    {
    Debug.Log("active self" + this.gameObject.activeSelf, this);
    Debug.Log("active in hierarchy" + this.gameObject.activeInHierarchy, this);

    //StartCoroutine(SinglePlayerFadeToBlackAndMoveBack(1.0f, 2.0f));
    }

    Here's my console with the function empty ((Debug lines commented out and inserted immediately prior to the function call)):

    http://imgur.com/a/Hg3vH

    And proof snippet:

    public void OnIppon()
    {
    // Debug.Log("active self" + this.gameObject.activeSelf, this);
    // Debug.Log("active in hierarchy" + this.gameObject.activeInHierarchy, this);

    //StartCoroutine(SinglePlayerFadeToBlackAndMoveBack(1.0f, 2.0f));
    }

    And that's the ONLY difference between the two console printouts. I reinsert the Debug.Log lines, the object often gets deactivated somehow. I comment them out, everything's fine. BTW my object has no parent. It always shows the active checkbox in the top left corner in the Inspector. I'm going to blow my brains out now.
     
  10. Deleted User

    Deleted User

    Guest

  11. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Looks like fecca solved it for you.
     
  12. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,992
    To sum up (based on that long vulgarly-named reddit thread) - there's nothing to see here. The apparent problem is an illusion due to an unrelated mistake.
     
    Bunny83 likes this.
  13. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    Just so the answer is here: Any root object with activeSelf true, but activeInHierarchy false is clearly a prefab. It is not an instance of a prefab, but the actual prefab itself. In the editor, this means its in the project view. In a build, this means it is one of the hidden, secret objects that unity creates for us behind the scenes.
     
    Bunny83 likes this.
  14. sawek1

    sawek1

    Joined:
    Aug 26, 2015
    Posts:
    2
    Or instead of adding reference to one of your child, You have added reference to prefab of this child. I had the same problem and that was my fault.
     
    MerKi likes this.
  15. unity_LW8HW0C1nzpw5w

    unity_LW8HW0C1nzpw5w

    Joined:
    Sep 27, 2018
    Posts:
    1
    (from more google searches)
    ActiveSelf (only read only): the active state of the object itself, corresponding to its inspector in the checkbox is checked ActiveInHierarchy (only read only): objects in the hierarchy is active. That is to say, to make the value true, the object and the activeself state of all its parent objects (and their ancestors) are true.

    An object to be visible in the scene (not hidden), then not only its own activeSelf to true, the activeself state of its parent objects (and their ancestors) true.

    Summary: The activeInHierarchy state represents the actual active state of the object in the scene. Actually represents the activeSelf state of objects and all their ancestors. And activeSelf corresponds to the checkbox in the inspector whether it is checked

    ActiveSelf state represents the state of the object itself, so when the object activeSelf is true, and the activeSelf state of the object's activeSelf state is not all true, the object's activeInHierarchy state is false."

    So what is an objects ancestor? The prefab from which it was instantiated? That has its active box checked in the Inspector...
     
  16. DavidKlecker

    DavidKlecker

    Joined:
    Jan 3, 2020
    Posts:
    15
    This is basically this issue for those still having this issue. DO NOT use prefabs in the creation of a tabbed area. I'm not sure what you can prefab, but it was the fact that my script was a prefab was causing issues with activating tabs. I would have to play around with things to see what can be prefabbed, but you cannot activate a prefab. So either make sure your variables are connected to the game objects in the hierarchy or call the game objects directly.