Say I have the following short script:
#pragma strict
class myArrayInfo
{
var name:String;
}
static var myArray:myArrayInfo[];
function Start()
{
myArray=new myArrayInfo[3];
myArray[0].name="Random name 1";
myArray[1].name="The answer to the ultimate question of life, the universe, and everything is...";
myArray[2].name="...42.";
}
I would expect that to work, but I get NullReferenceException: Object reference not set to an instance of an object
at line 13, where I attempt to set index 0 of the array.
So I tried doing this:
#pragma strict
class myArrayInfo
{
var name:String;
function myArrayInfo(){}
}
static var myArray:myArrayInfo[];
function Start()
{
myArray=new myArrayInfo[3];
myArray[0]=new myArrayInfo();
myArray[1]=new myArrayInfo();
myArray[2]=new myArrayInfo();
myArray[0].name="Random name 1";
myArray[1].name="The answer to the ultimate question of life, the universe, and everything is...";
myArray[2].name="...42.";
}
And this works. But why?! Why is it necessary to make it run an empty function (the constructor) to be assigned a value? The constructor clearly isn’t doing anything, unless I’m completely misunderstanding how constructors work.