Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Finding ALL Children of object

Discussion in 'Scripting' started by GoodNight9, Jan 28, 2017.

  1. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Hello guys. I'm trying to find all children in an object that contain a certain tag. Something similar to "GetComponentsInChildren<>()". But for tags instead of scripts. Everywhere I look basically gives me the following code. But it only finds immediate child objects, it does not go down the very last child (only 1 level down).

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class scr_fj_target_controller : MonoBehaviour
    7. {
    8.     public List<GameObject> tag_targets = new List<GameObject>();
    9.  
    10.     void Start ()
    11.     {
    12.         foreach (Transform child in transform)
    13.         {
    14.             if (child.gameObject.tag == "Targets")
    15.             {
    16.                 tag_targets.Add(child.gameObject);
    17.             }
    18.         }
    19.     }
    20. }
    21.  
    What can I use to find every child with a certain tag of an object?

    Thank you very much for your help guys. I really appreciate it-

    -Shade-[/CODE]
     
    astracat111 and Baccang like this.
  2. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    There's no pre-written function, but recursion can easily get you want you need. Just make a function that searches and adds a single level of children for a given parent, and then recursively call that function on each of the children as a parent of even deeper children in the hierarchy.

    Code (CSharp):
    1.     private void AddDescendantsWithTag(Transform parent, string tag, List<GameObject> list)
    2.     {
    3.         foreach (Transform child in parent)
    4.         {
    5.             if (child.gameObject.tag == tag)
    6.             {
    7.                 list.Add(child.gameObject);
    8.             }
    9.             AddDescendantsWithTag(child, tag, list);
    10.         }
    11.     }
    12.  
    13.     void Start()
    14.     {
    15.         AddDescendantsWithTag(transform, "Targets", tag_targets);
    16.     }
     
    angrypenguin, S1NATB, MaceB and 9 others like this.
  3. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Wow, you're freaking awesome man. Never would have thought of that.

    Much appreciate and thank you so much good Sir!
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Try using 'CompareTag(tag)' rather than '.tag == tag'.

    '.tag' actually generates a lot of garbage as it creates a new string every time you access it.

    It's just a quark in the way the mono runtime and the unity runtime interact. .tag has to read the data out of the unity runtime and turn it into a mono string.

    https://docs.unity3d.com/ScriptReference/Component.CompareTag.html
     
    ZO5KmUG6R and AndyGainey like this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    GetComponentsInChildren<Transform>() is also a useful way to get all of the children to iterate through.
     
  6. gray24

    gray24

    Joined:
    Aug 1, 2016
    Posts:
    14
    Try this. It found all childs, grandchilds, grandgrand, etc

    Code (CSharp):
    1.  private List<GameObject> AllChilds(GameObject root)
    2.     {
    3.         List<GameObject> result = new List<GameObject>();
    4.         if (root.transform.childCount > 0)
    5.         {
    6.             foreach (Transform VARIABLE in root.transform)
    7.             {
    8.                 Searcher(result,VARIABLE.gameObject);
    9.             }
    10.         }
    11.         return result;
    12.     }
    13.  
    14.     private void Searcher(List<GameObject> list,GameObject root)
    15.     {
    16.         list.Add(root);
    17.         if (root.transform.childCount > 0)
    18.         {
    19.             foreach (Transform VARIABLE in root.transform)
    20.             {
    21.                 Searcher(list,VARIABLE.gameObject);
    22.             }
    23.         }
    24.     }
     
  7. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,058
    thk
     
  8. snowinrain

    snowinrain

    Joined:
    Jan 17, 2016
    Posts:
    15
    My solution:
    Code (CSharp):
    1. foreach (Transform child in transform.GetComponentsInChildren<Transform>()) {
    2.     // Do what you want
    3. }
     
    Pr0x1d, DryreL, larry2013z and 4 others like this.
  9. LearnWARE

    LearnWARE

    Joined:
    Apr 19, 2019
    Posts:
    2
    I thought of this, googled, found your answer, tried it - worked! Thank you :)
     
  10. deliinteractive

    deliinteractive

    Joined:
    Oct 8, 2014
    Posts:
    25
    Rock solid answer! Thanks! I feel like a dummy for not thinking of it haha!
     
    GreenCubeGames likes this.