If I have a script that checks for a tag in a gameobject and I want to access a child in the gameObject with the tag what do I do? PLZ HELP
PLEASE DON'T ANSWER SOMETHING STUPID SUCK AS "Why don't you put the tag on the child?" or something like that, If you're gonna answer ANSWER MY QUESTION, I don't wanna know how to do it other way PLEASE UNDERSTAND I need this.
Easiest way would be : Get Child transform using its index , and then get GameObject of that child transform:
you can find parent gameobject using tag as per your script:
GameObject ParentGameObject = GameObject.FindGameObjectWithTag("Tag1");
GameObject ChildGameObject0 = ParentGameObject.transform.GetChild (0).gameObject;
GameObject ChildGameObject1 = ParentGameObject.transform.GetChild (1).gameObject;
OR
GameObject ChildGameObject2 = GameObject.FindGameObjectWithTag("Tag1").transform.GetChild (2).gameObject;
And so on …
I think there’s several ways to solve your problem, the easiest being:
use
Transform mychildtransform = gameobjectwithmytag.transform.FindChild(mychildname);
and then
mychildgameobject = mychildtransform.gameObject;
Of course there’s many ways to get your gameobjectwithmytag object. The simplest being
GameObject.FindGameObjectWithTag
or
GameObject.FindGameObjectsWithTag
if there’s many (of course you’ll need to iterate through them and extract the one you’re interested in).