void DestoryChild(GameObject parentObject)
{
Transform[] childList = parentObject.GetComponentsInChildren<Transform>(true);
if (childList != null)
{
for (int i = 1; i < childList.Length; i++)
{
if (childList[i] != transform)
Destroy(childList[i].gameObject);
}
}
}
위 함수처럼 작성을 하면 parentObject의 자식 오브젝트들을 모두 제거해준다.
여기서 for 문 내의 i값을 0이 아닌 1부터 시작하는 이유는 childList[0]에는 parentObject가 담겨있기 때문이다.
'Unity' 카테고리의 다른 글
[Unity] HP bar 만들기 (0) | 2021.04.06 |
---|---|
[Unity] 3D object 내의 Canvas가 카메라를 바라보도록 (0) | 2021.04.06 |
[Unity] SerializeField, Header 사용법 (0) | 2021.03.30 |
[Unity] Animator Trigger Parameters란? (0) | 2021.03.26 |
[Unity] Edit collider is not working (0) | 2020.10.04 |