본문 바로가기
IT/Dev

NullReferenceException: Object reference not set to an instance of an object

by sundancekidd 2014. 10. 10.

Gem이란 GameObject를 Instantiate해서 내부 Script component를 쓸 때,


변수 선언부에 

public List<Gem> gems = new List<Gem>();

이런식으로 하면 NullReferenceException! 발생. 


클래스 멤버 변수 초기화 문제로

위와 같은 식의 초기화는 start() 호출 시점에 할당 됨.


그러므로, 변수 선언부엔 

public List<Gem> gems = null;


그리고 초기화하는 곳에서

gems = new List<Gem>(); 으로 할당해 준다.