유니티 자식 오브젝트 찾는 법

  • transform.Find(string name);
  • transform.GetChild(int index);

대표적으로 두 가지가 있습니다.

Find(string name)


/* 아래 코드는 Transform 컴포넌트에 연결된 GameObject를 반환 */
//Cube
transform.Find("Cube").gameObject;

//Capsule
transform.Find("Capsule").gameObject;

//Cylinder
transform.Find("Cylinder").gameObject;

//Plane
transform.Find("Plane").gameObject;

Sphere의 자식 오브젝트를 Find 매서드로 찾는 경우입니다.

자식 오브젝트의 이름으로 자식을 검색하고 해당 게임 오브젝트의 Transform을 반환합니다.

만약 자식 오브젝트 중에 이름이 없으면 해당 이름을 가진 게임 오브젝트의 Transform을 반환합니다. 이름에 ‘/’ 문자가 포함된 경우에는 해당 이름을 가진 게임 오브젝트를 즉시 연결합니다.

GetChild(int index)


/* 아래 코드는 Transform 컴포넌트에 연결된 GameObject를 반환 */
//Cube
transform.GetChild(0).gameObject;

//Capsule
transform.GetChild(1).gameObject;

//Cylinder
transform.GetChild(2).gameObject;

//Plane
transform.GetChild(3).gameObject;

Sphere의 자식 오브젝트를 GetChild 매서드로 찾는 경우입니다.

해당 게임 오브젝트의 자식 오브젝트는 순서대로(0 부터) 정수 값을 가지며 정수 값에 해당하는 자식 오브젝트 Transform을 반환합니다.

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다