- Home /
[Custom editor] Having trouble getting list count inside a for loop
Hi
I am working on a custom editor and when I try to get a list count inside a for loop I keep getting a "NullReferenceException" error. The line it is having troubles with is
EditorGUILayout.LabelField("Count: " + gc.waves[i].enemies.Count);
'gc' is just my game controller and then waves are a list of a class, which contains a list of GameObjects called "enemies".
Now this line of code is in a simple for loop. This is basically how it looks like.
for (int i = 0; i < gc.waves.Count; i++)
{
EditorGUILayout.LabelField("Count: " + gc.waves[i].enemies.Count);
}
I have no idea what the problem is.
Any help is appreciated!
Answer by nasa8 · Dec 26, 2015 at 06:48 AM
some wave[i] = null or wave[i].enemies = null
first of all you should find out which element above is null and fix it or use code:
for (int i = 0; i < gc.waves.Count; i++)
{
if (gc.waves[i] != null && gc.waves[i].enemies != null)
{
EditorGUILayout.LabelField("Count: " + gc.waves[i].enemies.Count);
}
}
Thank you! I added this check a now it works without any problems!
Your answer
![](https://koobas.hobune.stream/wayback/20220612053906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Call EditorGUILayout.PropertyField of an element of an array 0 Answers
Editor: How to do PropertyField for List elements? 4 Answers
C# Displaying List Elements in Multiples 1 Answer
List.Add just once in Update 2 Answers