27

I want to say somethings like..

Gameobject.find(child gameobject of specific parent Gameobject)

Can anyone help me. thanks!

Community
  • 1
  • 1
  • Look at this http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html – Imapler Sep 10 '14 at 10:57
  • [There is even a find function on the transform](http://docs.unity3d.com/ScriptReference/Transform.Find.html) – T. Kiley Sep 10 '14 at 10:57

5 Answers5

56

GameObject.Find will search for a gameobject in the scene. To search a gameobject from a parent, use Transform.

There are 2 ways of doing it:

  1. transform.Find("childname")
  2. transform.FindChild("childname")

The 2nd option is deprecated but still functional, so you'd better use the 1st option.

demented hedgehog
  • 7,007
  • 4
  • 42
  • 49
Jay Kazama
  • 3,167
  • 2
  • 19
  • 25
  • 4
    when i am trying this code.i found one error like..."Cannot convert type `UnityEngine.Transform' to `UnityEngine.GameObject" –  Sep 10 '14 at 11:42
  • 7
    it is because `Transform.find("childname")` returns a type of Transform. If you want to get the gameobject, just add .gameObject at the end: `Transform.find("childname").gameObject` – Jay Kazama Sep 10 '14 at 11:44
  • I made user3164248's changes to this answer (just to avoid confusion). – demented hedgehog May 26 '15 at 04:14
6

If a GameObject are you looking for in hierarchy it must be like:

transform.Find("head/eyes")
transform.FindChild("head/eyes")
zxmaster
  • 61
  • 1
  • 2
  • 2
    Thank you! The "Find()" call was returning NULL for me and I saw in the Unity docs that it doesn't traverse the hierarchy (to get sub-children) by default. Using the forward slash allows you to do that (e.g. "ParentName/ChildName/GrandchildName" to get "GrandchildName"). – clamum Jul 09 '20 at 04:12
5

Fixing Jay Kazama's answer. The correct answers are:

  1. transform.Find ("childname")
  2. transform.FindChild ("childname")

With small t (property transform, not class Transform).

user3164248
  • 155
  • 3
  • 8
4

For answers above stating transform.FindChild("childname") as Answer, this is to inform you that transform.FindChild("childname") is deprecated.

Use this, this will work as expected

transform.Find("childName");

if you want to find Child of a GameObject by name, use this,

GameObject head = HeadPanel;    // just for reference
head.transorm.Find("childName").gameObject;
Junaid Pathan
  • 3,850
  • 1
  • 25
  • 47
1

You can do this by GetChild(index of child members)

  • 1
    The bad thing about using the GetChild (index) is that if you change the order of the objects you lose the reference to them – Jaime Roman May 21 '19 at 09:21