- Home /
Access a child from the parent or other gameObject.
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.
chill down... It's okay to ask for an exact answer to your question but do you have to do it in such an agitated manner? Greetz, $$anonymous$$y.
Answer by chetanisinanand · Jul 23, 2014 at 06:37 PM
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 .....
Answer by roamcel · Aug 07, 2011 at 09:23 AM
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).
Your answer
Follow this Question
Related Questions
GameObjectWithTag Child 1 Answer
Make a simple tree 1 Answer
Display Name Above Object 2 Answers
how do I get reference to a parent or a child? 2 Answers
How to check parent value instead name value(more details in post) 3 Answers