
Unity 배움의 초장에서 if문을 배우다가 알게 된 메서드이다.
게임 오브젝트를 만지고 갖고 놀고 그런 것과는 달리
오브젝트를 아예 파괴한다는 느낌이라 위험할 것 같기도 하고 호기심이 생겼다.
(위험한 것은 재밌어 보이기 마련이다)
모르는 부분은 빨간색으로 강조했다.
(Unity Documentation의 Scripting API: Object.Destroy 참조함.)

Destroy 메서드는 Object 클래스의 메서드다. 게임오브젝트(GameObject), 컴포넌트(Component) 혹은 에셋(Asset)을 제거(Remove)한다고 한다.
The object obj is destroyed immediately after the current Update loop, or t seconds from now if a time is specified. If obj is a Component, this method removes the component from the GameObject and destroys it. If obj is a GameObject, it destroys the GameObject, all its components and all transform children of the GameObject. Actual object destruction is always delayed until after the current Update loop, but is always done before rendering.
- 오브젝트 obj는 현재 업데이트 루프 혹은 (시간이 특정되었다면) 지금으로부터 t초 뒤에 즉시 파괴된다.
- 이 메서드는 GameObject의 Component를 제거하고 파괴한다.
- 만약 obj가 GameObject라면 GameObject와 GameObject의 컴포넌트와 자식들도 파괴한다. 실제 오브젝트 파괴는 항상 업데이트 루프 뒤로 밀린다. 하지만 항상 렌더링 전에 완료된다.
Note: When destroying MonoBehaviour scripts, Unity calls OnDisable and OnDestroy before the script is removed.
- 노트: MonoBehaviour 스크립트를 파괴할때, 유니티는 OnDisable과 OnDestroy를 호출한다.
(Unity Documentation의 Creating and Destroying GameObjects 참조함.)
Destroy 함수는 게임 오브젝트 자체에 영향을 주지 않으면서 개별 컴포넌트를 파괴할 수 있습니다. 일반적인 실수는 다음과 같이 작성하는 것입니다.
Destroy(this);
…이것은 스크립트가 연결된 게임 오브젝트를 파괴하는 것이 아닌, 호출하는 실제 스크립트 컴포넌트를 파괴합니다.
OnDisable과 OnDestroy가 뭐지? -> 검색
MonoBehaviour의 메서드였다.
OnDisable = Behaviour가 비활성화 될때 호출되는 메서드
OnDestroy = MonoBehaviour가 파괴 될때 호출되는 메서드
Awake(), Start(), Update(), FixedUpdate(), OnEnable()과 비슷한 결의 함수라고 한다.
소감 :
티스토리 블로그 서체랑 크기가 들쑥날쑥하다.
더 공부할것 :
- GameObject / gameObject
- MonoBehaviour
'공부 > Unity' 카테고리의 다른 글
| 유니티에서의 Sleep() 함수 (0) | 2024.10.02 |
|---|