A bizarre technical error.

Hello,
I have a script that uses the ‘.Count’ property of a List variable, inside a ‘for loop’ (to determine the count of the loop), and when I run the program, I get an error FROM my for loop that says…

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

The List is NOT ‘empty’ or ‘null’ (when I print it out BEFORE the ‘for loop’ starts), so, I decided to store the ‘.Count’ property value inside an integer, and THEN put the integer inside my for loop, and I get the error from the code that is INSIDE my for loop, instead of FROM the for loop. I wonder WHY that is happening.

I fixed the problem.

does the for loop like this?
it’s the < that’s important. a List<> starts at 0
so if you have 5 object in the list, it’s 0,1,2,3,4
bu Count says 5

for(int i = 0; i < numEnemies.Count; i++)
        {
            Debug.Log("Creating enemy number: " + i);
        }
2 Likes

No. I found out what the problem was. It was a referencing problem.