0

I have an unusual problem with my List. The code looks like this:

tanks = new List<Tank>();

tanks.Add(new Tank("Player", position1, false));
tanks.Add(new Tank("Enemy", position2, true));

if (tanks[0] == null) Debug.LogError("tanks[0] == null");

For some reason the error appears. Why does it happen? If I call List.Count, it returns 2... Can someone help me?

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
  • 1
    That is all you have for sure? You don't have an extra line between the if statement and the debug? I asked because you don't use {} after your if so if you have an extra line in between, it will be skipped and your debug gets printed regardless the result of the check. – Everts Oct 22 '17 at 15:03
  • There's nothing between those lines, except some instructions that create other lists. –  Oct 22 '17 at 15:21

2 Answers2

1

As we can see on this example your code works perfectly. This means that the code you provided does not have the error you talk about.

What is most definitely happening is what @Everts talked about in the comments, your Debug.LogError("tanks[0] == null"); line is probably not within the if block in your code. Try adding brackets {}around it.

here is your updated code

tanks = new List<Tank>();

tanks.Add(new Tank("Player", position1, false));
tanks.Add(new Tank("Enemy", position2, true));

if (tanks[0] == null) 
{
    Debug.LogError("tanks[0] == null"); 
}
Mathieu VIALES
  • 4,526
  • 3
  • 31
  • 48
  • I think that it might be Unity issue. I know that the code should work, but it still doesn't. –  Oct 22 '17 at 15:22
  • can you show us the whole function ? Copy/pasted directly from your IDE ? I can't believe unity and whatever compiler it uses could fail at something as simple as a null check ... – Mathieu VIALES Oct 22 '17 at 15:22
  • Actually, I commented everything except those 4 lines in the function, and the error is even more weird now - it shows that the list is a null reference, although it's created in the line before. –  Oct 22 '17 at 15:26
  • uh ... it's getting even weirder -_- Can you give us a screenshot ? Your code sounds fun ;-) – Mathieu VIALES Oct 22 '17 at 15:27
  • Of course, I will upload them asap and post there –  Oct 22 '17 at 15:27
  • My bad, the null reference wasnt refering to the list, but to the argument that was not initialized. I uncommented one line that initializes it, and the error is still the same as in the beginning –  Oct 22 '17 at 15:34
  • What are the lines ? Is your "if" on the line 30 ? if you put a break point right before the if is hit (so on the if's line) and that you inspect the tanks var ? – Mathieu VIALES Oct 22 '17 at 15:34
  • Mind giving an updated screenshot ? What's the line that you uncommented ? Was it sown in the screenshot ? – Mathieu VIALES Oct 22 '17 at 15:35
  • I have found the problem. I will never disable warnings since now xD One of them was saying: Creating a Monobehaviour by ,,new" keywoard is not allowed. Everything is clear now. Thanks for your help! –  Oct 22 '17 at 15:39
  • haha ! Glad i could help :-) – Mathieu VIALES Oct 22 '17 at 15:40
0

For people who might have the same issue: creating Monobehaviours using ,,new" keyword is not allowed and it will cause an error. Just enable your warnings ;)