- Home /
How to get the childrens tag from the parent GameObject?
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
if(calcSpd.turnTable[0].tag == "Player"){
}
How are you assigning that array?. BTW Technically this is not a parent/child thing. For that, you would be using
for (var child in transform) { if child.tag == ....
Answer by aldonaletto · Nov 04, 2011 at 11:00 PM
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 } }
Thank you aldonaletto :3 this was just what I was looking for ^^