Search Unity

Get all the childs in Transform even if the childs are inactive???

Discussion in 'Scripting' started by Ted-Chirvasiu, Aug 5, 2011.

  1. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    Hello!I was struggleing to find a way to get all the childs in my main parent (this means the childs in the parents that are parented to the main transform as well) even if they are inactive...

    Code (csharp):
    1. var allChildren : Transform[]  = gameObject.GetComponentsInChildren.<Transform>() as Transform[];
    2.  
    3. OR
    4.  
    5. var allChildren = gameObject.GetComponentsInChildren(Transform);
    would work but some of my childs are inactive and it seems that it can't get them :(

    A simple loop like :

    Code (csharp):
    1. for(var x : Transform in transform){
    2.  
    3. //won't get all the children,only the first "sets"
    4.  
    5. }
    Please help me!
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You can make it recursive meaning that it calls itself for all the children it finds (which then calls it for those children, etc, etc), adding it to a list of some sort.
     
    MicKre likes this.
  3. joshimoo

    joshimoo

    Joined:
    Jun 23, 2011
    Posts:
    266
  4. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
  5. joshimoo

    joshimoo

    Joined:
    Jun 23, 2011
    Posts:
    266
    The bool includeInactive is just an additional parameter with a default value of false.
    Which means if you don't pass it false is used, but if you pass your own value than that is used.

    Example:
    Code (csharp):
    1.  
    2. gameObject.GetComponentsInChildren<Transform>(true);
    3. gameObject.GetComponentsInChildren(Transform, true);
    4.  
     
    Last edited: Aug 5, 2011
  6. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    Thank you very much!!! :D
     
  7. kamullis

    kamullis

    Joined:
    Feb 7, 2013
    Posts:
    21
    Does this method not work any more? The syntax for the second one gives me an error and the first one doesn't seem to be working.

    Just to explain I am trying to enable all disabled colliders when the user clicks a gui button

    This is the code I am trying to get working
    Code (csharp):
    1.  
    2. public Collider[] colliders;  
    3.  
    4. public void collidersOn(){
    5.         colliders = gameObject.GetComponentsInChildren(Collider, true);
    6.         foreach (Collider c in colliders) {
    7.             c.enabled = true;
    8.         }
    9.     }
    10.  
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    In line 5, instead of "Collider" as the first argument (that is actually a type), instead pass this object's transform, which is the lowercase transform, like so:

    Code (csharp):
    1.  
    2. colliders = gameObject.GetComponentsInChildren( transform, true);
    3.  
    That is assuming the children you are looking for are on the object that this script is on.
     
  9. kamullis

    kamullis

    Joined:
    Feb 7, 2013
    Posts:
    21
    Actually using lowercase transform throws an error and I want the colliders anyway. The problem was that I was asigning the array incorrectly for the foreach function incorrectly. It seams that setting includeInactive to true is not necessary for my purposes. In the documentation (http://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html) it makes it seem like it is referring to inactive components as opposed to inactive gameobjects. Yet, whether it's set to true or not i am able to access inactive components.

    I was simply making a mistake in how I was assigning objects to the array.
     
  10. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    I ended up having to do this:

    Code (CSharp):
    1. Transform[] children = parentGameObj.GetComponentsInChildren(typeof(Transform), true) as Transform[];
    Actually, no wait...that gave me errors, this is indeed the correct code:

    Code (CSharp):
    1. Transform[] children = parentGameObj.GetComponentsInChildren<Transform>(true);
     
    Last edited: Feb 17, 2019
    Bunny83, cloverme and drhmiri like this.
  11. WaqasGameDev

    WaqasGameDev

    Joined:
    Apr 17, 2020
    Posts:
    118
    I was using same function but I did not notice there is an overload method also. So simple it was to find inactive object. Working perfectly. Thanks.
     
  12. MaxLohMusic

    MaxLohMusic

    Joined:
    Jan 17, 2022
    Posts:
    67
    Man, it's honestly incredible how such a simple solution gets forgotten over the years. I saw another similar question and the "solution" was to make a holder of the game object to make sure it doesn't become inactivated
     
  13. MaxLohMusic

    MaxLohMusic

    Joined:
    Jan 17, 2022
    Posts:
    67
    Now how do we get only the top-level transforms? This returns all of them recursively
     
  14. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,983
    This question is over 11 years old and the answer to your question (which is not what this thread was about) is actually in the very first post:

    This gets the direct children only.
     
  15. MaxLohMusic

    MaxLohMusic

    Joined:
    Jan 17, 2022
    Posts:
    67
    No, I think you are forgetting the whole point of the original post: We want to get inactive children as well.

    That version only gets ACTIVE children but we want *top-level* children *including* inactive.

    The top-voted comment gets all children including inactive but includes nested children. And the 1st comment gets top-level children but excludes inactive. So how do you get top-level children and include inactive?

    FWIW, I have resorted to using a Where clause filtering them by the name
     
  16. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,983
    I wasn't answering the OP since that was answered above. The OP wanted to get all children including nested children. That's why he needs to use GetComponentInChildren with the second argument set to "true" to include inactive object.

    The necro poster above me I replied to only wanted to get all direct top level children only and the simple foreach loop does this. It does not exclude inactive children. So I'm not sure what you're talking about. The OP mentioned that the simple for each loop does not include nested objects which the OP wanted.

    If YOU think about getting the top level gameobject (note: gameobjects, not children) in the scene, Unity's SceneManager mechanic using GetActiveScene and GetRootGameObjects would be the way to go.
    Though in that case we don't talk about children anymore as they don't have any parent.

    ps: Yes, in the past, before we got the SceneManager the only way to get all top level objects in the scene was to use something like FindObjectsOfType to get all Transforms and then just filtering out everything that has a parent. What's left was only the top level objects in the scene. This was very inefficient. Since we have multi scene editing and the SceneManager, it has become much simpler.