List returns NullReferenceException, but when it's visible in the Inspector it works perfectly ???
Hi fellow developers
I have a weird issue. I have a collection of lists, that keeps track of my enemies in my game. Whenever I play the game, with my lists shown in the Inspector sidepanel, they work as intended. But when my inspector is showing anything else, the lists give me NullReferenceExceptions.
Is this a bug or what could cause this behavior?
You mean the code remains the same, you simply select something else in the Editor? And it only works when the inspector displays the Lists?
When you say collection of Lists, you mean List<GameObject> []
?
Answer by Bunny83 · Jul 15, 2017 at 03:30 PM
The inspector will automatically create an instance of a List or array when viewed. At least any Array or List that is serialized. If you fill / create the list dynamically at runtime you have to create the list manually.
You should inlude the declaration of your List variable(s). Also the spot where you get the NullReferenceException and maybe the part where you initialize your list(s) is you even do this.
Your answer
![](https://koobas.hobune.stream/wayback/20220612130053im_/https://answers.unity.com/themes/thub/images/avi.jpg)