- Home /
First child of a gameobject
GameObject first = GameObject.Find(ObjName+"-parent");
i have lot of chidren for a gameobject first . how to get first child in C#
Answer by Chronos-L · Jun 03, 2013 at 07:06 AM
Transform [] t = GameObject.Find("obj1").GetComponentsInChildren<Transform>();
Transform theParent = t[0];
Transform firstChild = t[1];
This should work.
As a single line:
Transform firstChild = GameObject.Find("obj1").transform.GetChild(0);
You could write this simply in one line
Transform child = GetComponentInChildren<Transform>();
@Ash-Blue is incorrect, but his confusion is understandable. Although the name doesn't make this clear, if you call componentInstance.GetComponentInChildren
it will return the Transform of componentInstance
– not the Transform of the first child!
If you want the first child of a Transform, then transformInstance.GetChild(0)
is what you should use.
Answer by apantev · Jan 06, 2015 at 03:33 AM
Transform firstChild = GameObject.Find("obj1").transform.GetChild(0);
This is the answer ThinkyHead_ gave in a comment but I'm putting it in a proper answer, because I don't want people to miss it.
Answer by Hattoriseed · Mar 01, 2019 at 02:19 PM
This is super old, but the question was how to get the first GameObject, so the right answer should be GameObject first = gameObject.transform.GetChild(0).gameObject;
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Multiple Cars not working 1 Answer
Respawn timer? Duplicate instance OnDestroy question... 0 Answers
Parent object to child of another? 1 Answer