[Unity] 객체의 자식들에 접근하기

yesman·2021년 8월 27일
0

Unity

목록 보기
4/9

GetComponentsInChildren<접근할 컴포넌트>() //배열로 반환됨

diary의 자식으로 되어 있는 4개의 객체 모두 text인데,

gameObject.GetComponentsInChildren<Text>()

이런 식으로 접근하였더니 배열로 [0]부터 [3]까지
sequence, content, sequence (1), content 순으로 저장되었다.

난 sequence와 sequence (1)의 값만 바꿔주고 싶어서

gameObject.GetComponentsInChildren<Text>()[0].text = "#" + num.ToString();
gameObject.GetComponentsInChildren<Text>()[2].text = "#" + num2.ToString();

이런식으로 접근하였더니 잘 작동되었다.

결론

gameObject.GetComponentsInChildren<>() 은 배열로 반환되며 객체의 아래에 있는 모든 자식들을 가져온다.

profile
유니티

0개의 댓글