Search Unity

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:
    1,066
    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,355
    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
     
    TeacherSteven and Lylek like this.
  9. Meltdown

    Meltdown

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

    Code (csharp):
    1. foreach (Transform child in transform)
    2.             child.gameObject.renderer.enabled = false;
     
  10. Deleted User

    Deleted User

    Guest

  11. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    I like how there is no GetChildren method. Its like.. What?
     
    rahulk1991 likes this.
  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:
    53
    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.
     
    albertyllo90 likes this.
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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:
    6,336
    Also it's not related to the original question. Don't bump this!