Search Unity

Get children using a tag

Discussion in 'Scripting' started by Inthemindofforrest2, Apr 3, 2019.

  1. Inthemindofforrest2

    Inthemindofforrest2

    Joined:
    Nov 23, 2018
    Posts:
    1
    Why does unity not have this function yet?

    Code (CSharp):
    1. GameObject GetChildWithTag(GameObject Obj, string tag)
    2.     {
    3.         for(int i = 0; i < Obj.transform.childCount; i++)
    4.         {
    5.             if(Obj.transform.GetChild(i).tag.CompareTo(tag) == 0)
    6.             {
    7.                 return Obj.transform.GetChild(i).gameObject;
    8.             }
    9.         }
    10.         return null;
    11.     }
    12.     List<GameObject> GetChildsWithTag(GameObject Obj, string tag)
    13.     {
    14.         List<GameObject> Children = new List<GameObject>();
    15.  
    16.         for (int i = 0; i < Obj.transform.childCount; i++)
    17.         {
    18.             if (Obj.transform.GetChild(i).tag.CompareTo(tag) == 0)
    19.             {
    20.                 Children.Add(Obj.transform.GetChild(i).gameObject);
    21.             }
    22.         }
    23.         return Children;
    24.     }
    Is it too expensive?