[Unity] this와 gameObject의 차이점

양유성
1 min readJan 19, 2019

--

GameManager와 같은 클래스의 인스턴스는 하나의 게임에 여러개가 있으면 안될 때가 있다.

이럴 때, 싱글턴 지정을 해줘야하는데, static을 통해서 수행할 수 있다.

if (instance == null){

instance = this;

}else if (instance != this) {

Destroy(gameObject);

}

뭐 위와 같은 식으로…

근데 여기서 Destory(gameObject)와 Destroy(this)가 헷갈릴 수 있는데, 차이점은 다음과 같다.

this는 Component다. 만약 Player.cs라는 스크립트가 있다면 this는 Player 컴포턴트를 의미한다.

gameObject는 GameObject이다. Player.cs라는 스크립트 속, class Player를 통해 객체화된(Instantiate) 오브젝트를 의미한다.

따라서 Destroy(this)를 하면, 컴포넌트만 없어지고(destroy), Destroy(gameObject)를 하면 오브젝트와 해당 오브젝트를 통해 상속되고 만들어진 다른 오브젝트가 모두 없어진다.

--

--