I have a weird problem. I am using a editor window. With this window I am generating an array of enemies. The array I’m using is the Unity’s built-in array.
Here is the simplified version of my code:
class Enemy {
var health : int;
}
var enemies : Enemy[];
enemies = new Enemy[5];
// assign a value to one of array's elements
enemies[0].health = 10;
In the last line I get an error that says object reference is null. I used debug.log and printed a message if array is null. And another message telling array’s length.
It seems that the array is null, but it does return enemies.length as being “5”. In the inspector I can see there are 5 elements, but the values are not assigned.
But when I assign each element one by one:
for(i=0; i<5; i++)
{
enemies *= new Enemy();*
}
It works properly.
What am I missing? Why doesn’t this work. Util now I had not run into such an issue. This code is inside an objects script, I call it from my custom window BUT, it also doesn’t work when I call it from object’s script in runtime.