Hey Unity community
I’m currently working on a RPG game, but I ran into the problem of having multiple tags, so I created empty gameobjects and gave them the other tag, but now I need to access the child of a gameobject, and I looked it up, but didn’t seem to get the answer I needed
here’s my code:
var turnTable : GameObject;
// then I add the values to the turnTable
//and here I need to access the tag of the child, currently it’s just accessing the gameobjects tag
That’s a good question: after searching around, I concluded that a Transform doesn’t allow direct access to its children array! You can iterate through all children using for (like @DaveA suggested), but there’s no such a thing like transform.children[].
If you have several objects childed to every turnTable element, and need to find the ones tagged Player (or other particular tag), you can use this:
for (var child in turnTable[0].transform){
if (child.tag == "Player"){
// Player found, and child is its transform
// do whatever you want with this object
}
}