[Unity] List<> 사용 중에 NullReferenceException 오류 떴을 때

jh Seo·2022년 8월 24일
1

유니티

목록 보기
7/42

개요

List<TextMeshProUGUI> buttonTexts;

UI용 TMP의 list를 선언해서

TextMeshProUGUI temp = elem.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>();
buttonTexts.Add(temp);

이런식으로 넣어주는 코드였는데
NullReferenceException: Object reference not set to an instance of an object
갑자기 이게 떠서 당황했다.

원인

C#언어에 익숙치 않아서 생긴 오류다.
List를 선언만 하고 new연산자를 통해 초기화를 안해서 나는 오류였다.

buttonTexts = new List<TextMeshProUGUI>();

초기화했더니 정상적으로 되었다.

profile
코딩 창고!

0개의 댓글