How can I find child gameobject?

别等时光非礼了梦想. 提交于 2019-12-04 00:13:25
Jay Kazama

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.

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).

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

transform.Find("head/eyes")
transform.FindChild("head/eyes")

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;

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!