- Home /
finding a child object by string
Hi, I need to do a simple thing. Given any GameObject A, I need to obtain the GameObject B who is a child of A and its name match a string passed as parameter. Now, GameObject class provides a GetComponents method which returns all children of a given type. I suppose i could do a A.GetComponents(GameObject) but c# compiler complains because GameObject is not a Component. As an example, take the Car tutorial. The car object has a WheelFL child. I need a method to obtain this WheelFL object using only its name and not its type.
Any help is appreciated
Answer by Mike 3 · Jul 14, 2010 at 08:49 AM
transform.Find("WheelFL") will find a direct child called WheelFL
If you need a recursive find, you'll need to code it yourself though
so something like this will work?
GameObject Car; Car.transform.Find("WheelFL").ganeObject;
With the typos fixed, yup (as long as the wheel is a direct child as i mentioned)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
C# GameObject.FindGameObjectsWithTag Children 1 Answer
Show and Hide a prefab or GameObject 10 Answers
Script sharing 2 Answers