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

New GameObject active question

Discussion in 'Developer Preview Archive' started by Crazy Robot, Sep 10, 2012.

  1. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    When I call:

    Code (csharp):
    1. GameObject.SetActive(true);
    It only activates the gameobject itself not all of the children. How do I get it to activate all of the children as well?

    Thanks

    JL
     
  2. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    This is not a bug.
    To activate all objects you must use SetActiveRecursively .
     
  3. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    That is deprecated in 4.0. I get a yellow caution with it.

    This is what the release notes state:

     
  4. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    I wasn't aware of it, thanks for posting this.
     
  5. seethesks

    seethesks

    Joined:
    Jul 20, 2012
    Posts:
    2
    I found this really confusing also.

    To get it working, make the child objects active first, either in the prefab or in the scene. Next turn off the parent, which now turns off the child objects also.

    When you call SetActive on the parent whichever child objects you had previously turned on will now also become active.
     
  6. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    Thanks for the reply,

    Man, I'm confused.

    How do I turn on/off a GameObject with all the children? What replaced SetActiveRecursively?

    Lets say I want to turn on a Gameobject that was 50 child objects.

    Do I have to first turn on all of the 50 child objects separately then the parent?


    Do I have to have them all on at the beginning of the scene and at "Start" turn them all off so they will all turn on later on when I need them?


    This seems like a step backward in ease of game making.
     
    Last edited: Sep 11, 2012
  7. BigB

    BigB

    Joined:
    Oct 16, 2008
    Posts:
    672
    This seems a huge step back.
    So, they removed the function SetActiveRecursively, and there's no replacement for it ? We need to browse the entire gameobject and do it manually now ?
     
  8. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    I'm not sure,

    I have to really play with it to figure it all out. I certainly hope that's not how it works. It would be nice if someone from Unity would weigh in and let us know how this works.
     
  9. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    When you turn off the parent, even if the child is on, Unity won't run child's components.
    But you can still check if it is on in hierarchy using '.activeInHierarchy', though that doesn't mean the child is doing anything if the parent is turned off.

    When you turn on/off the parent, if the child was already off, when you turn on the parent, that child won't come active too. You would need to set it active in hierarchy. Just don't start the scene with objects already off.
     
    Last edited: Sep 11, 2012
  10. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    Ok, so how about turning a gameobject on that has children?

    I have a parent GameObject that's normally off with a bunch of children (all off). When I want to activate it, I called "GameObject.SetActive(true)".
    Only the parent turned on, how do I get the entire group to turn on?

    It sounds like I have to have them all on at the beginning of the scene, then turn them off under Awake or Start, then turn them on when I want them?

    Is that right?
     
  11. Jtbentley_v2

    Jtbentley_v2

    Joined:
    Sep 5, 2012
    Posts:
    174
    I'm curious to know the reasoning behind this.. There's obviously some sort of epic optimisation that this allows that I'm not aware of?
     
  12. JJC1138

    JJC1138

    Joined:
    Feb 23, 2012
    Posts:
    89
    You start with the children on (active) but the parent off. Because of the new rules, the children will act as if they are inactive as well when the parent is inactive. Then when you SetActive(true) the parent that will allow the children to run too.
     
  13. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921

    WOW!! This is completely crazy! I guess I have to rethink how gameobjects are handled. Normally one would think if a gameobject is active, whether it's a child or not, is on. So even though a gameobject is turned on if it is a child and the parent is off, then it's off....

    WOW!
     
  14. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    Leave the children on. Turn off only the parent. You have the same effect of turning all of them off.
    ooops, didnt see JJC1138s post.
     
  15. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    I think I prefer this new way of working, but there's at least 1 big migration issue... In the past, we would activate or deactivate a GameObject in the scene editor and it would ask "propagate to children" (or something like that).

    So, if like in my case, you have a full body of deactivated GameObjects, now you have to manually go through them all and activate 1 by 1, since this feature is not there any longer.

    And this is how I've just fixed it:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. /// <summary>
    6. /// Set all children active, for migrating from Unity 3
    7. /// </summary>
    8. public class SetAllChildrenActive {
    9.     [MenuItem ("GameObject/Set All Children Active")]
    10.     static void DoIt () {
    11.         Selection.activeGameObject.SetActiveRecursively(true);
    12.     }
    13. }
    14.  
     
    Last edited: Sep 19, 2012
  16. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    Unity now supports multi-selection. You can simply select all of them and hit the active toggle once.