- Home /
Question by
Ochreous · Jun 26, 2013 at 09:00 PM ·
c#childrenfindgameobjectswithtag
C# GameObject.FindGameObjectsWithTag Children
Hi everyone, I have two gameobjects that are Tagged and I want to access their children. How exactly do I do that? I know there's a way to find the gameobjects with GameObject.FindGameObjectsWithTag and you can access children with GetComponentsInChildren(); but I'm not sure if you can combine the two.
public Transform someGameObjectChildren;
public Transform someGameObjectChildren2;
void Awake(){
//pseudo code below
//someGameObjectChildren = GameObject.FindGameObjectsWithTagAndChildren(someGameObject.children);
//someGameObjectChildren2 = GameObject.FindGameObjectsWithTagAndChildren(someGameObject2.children);
}
Comment
Best Answer
Answer by DocteurCox · Jun 26, 2013 at 09:09 PM
You could just do a function that combine both :
static T[] FindObjectByTagAndComponent<T>(string tag)
{
GameObject go = FindGameObjectWithTag(tag);
T[] children = go.GetComponentsInChildren<T>();
return children;
}
Hope it helps :)
I'm getting an error from the console saying local variable go cannot be declared.
Oups, just a little mistake. I fixed it. Sorry about that :p