Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How to Get All children in a GameObject?

Discussion in 'Scripting' started by Viskag, Jun 19, 2009.

  1. Viskag

    Viskag

    Joined:
    Apr 20, 2009
    Posts:
    46
    Here is GameObject named "xxx",and I don't know the names of its children.How can i set its children invisible?I failed like this:
    Code (csharp):
    1. Renderer[] ChildrenRenderer = ObjParent.GetComponentsInChildren(typeof(Renderer)) as Renderer[];
    2. foreach(Renderer abc in ChildrenRenderer )
    3.   abc.enabled=false;
    why?
     
  2. Slem

    Slem

    Joined:
    Jan 28, 2009
    Posts:
    191
    Thats because safecasting('as') doesnt handle arrays to well in my experience. GameObject.GetComponents() returns a Components[]. Just rewrite your this line
    Code (csharp):
    1. Renderer[] ChildrenRenderer = ObjParent.GetComponentsInChildren(typeof(Renderer)) as Renderer[];
    to

    Code (csharp):
    1. Renderer[] ChildrenRenderer = ObjParent.GetComponentsInChildren(typeof(Renderer));
    That should do it.
     
    ProbePLayer likes this.
  3. Viskag

    Viskag

    Joined:
    Apr 20, 2009
    Posts:
    46
    i delete "as Renderer[]",it says:

    Assets/Script/main.cs(66,24): error CS0266: Cannot implicitly convert type `UnityEngine.Component[]' to `UnityEngine.Renderer[]'. An explicit conversion exists (are you missing a cast?)

    and when i add "as Renderer[]",it says:

    NullReferenceException: Object reference not set to an instance of an object
     
  4. Viskag

    Viskag

    Joined:
    Apr 20, 2009
    Posts:
    46
    still How to Get All children in a GameObject?forget the Renderer....
     
  5. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    304
    The Transform class is IEnumerable, so simply loop over it to get all child transforms:
    Code (csharp):
    1. // Moves all transform children 10 units upwards!
    2. for (var child : Transform in transform) {
    3.     child.position += Vector3.up * 10.0;
    4. }
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,349
    I think Slem made a typo. Because GetComponents() returns Component[], ChildrenRenderer has to be of type Component[]. The foreach loop will still work as written, because it will cast each Renderer properly as it iterates.
     
  7. Viskag

    Viskag

    Joined:
    Apr 20, 2009
    Posts:
    46
    thx!
     
  8. Trafalgar Lew

    Trafalgar Lew

    Joined:
    Apr 2, 2013
    Posts:
    4
    Code (csharp):
    1. GetComponentsInChildren<Collider>();
    That's the correct way
     
    Lylek23 likes this.
  9. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,353
    My favourite C# way...

    Code (csharp):
    1. foreach (Transform child in transform)
    2.             child.gameObject.renderer.enabled = false;
     
    Shippety and Rodolfo-Rubens like this.
  10. technicat

    technicat

    Joined:
    Nov 22, 2006
    Posts:
    1,237
  11. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    445
    I like how there is no GetChildren method. Its like.. What?
     
  12. PaulGibson

    PaulGibson

    Joined:
    Mar 28, 2017
    Posts:
    2
    How can one do this in the editor scripting side, where there is no transform property? I have an fbx model that I want to do something to all of its children, but there are too many to do it all manually.
     
  13. KeinZantezuken

    KeinZantezuken

    Joined:
    Mar 28, 2017
    Posts:
    40
    Gonna bump this.

    Anyone knows how to find Children gO attached to specific named gO?

    This:

    Code (CSharp):
    1. GameObject g4 = GameObject.Find("MyMainGO");
    2.             if (g4 != null)
    3.             {
    4.                 foreach (object obj in g4.transform)
    5.                 {
    6.                     Transform child = (Transform)obj;
    7.                     Debug.Log("Children is: " + child.name);
    8.                 }
    9.             }
    10.         }
    Does not work properly.
     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    15,804
    Why? This thread is so out of date as to be essentially useless. A lot has changed since 2009. Start a new thread.
     
  15. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,406
    Also it's not related to the original question. Don't bump this!