Static empty arrays are null an nonstatic aren't null?

When trying to create an empty array to add a value to an array I get a error when creating the temp array…
“ArgumentNullException: Argument cannot be null.”

If I try this:

static var foo : Item[];

function Add()
{
	var tempArr = new Array(foo); // nullreference
	
	tempArr.Add(new Item());
	
	foo = tempArr.ToBuiltin(Item);
	
	print("foo'ed");
}

But if I try this:

var foo : Item[];

function Add()
{
	var tempArr = new Array(foo);
	
	tempArr.Add(new Item());
	
	foo = tempArr.ToBuiltin(Item);
	
	print("foo'ed");
}

I know I can check if it’s not empty before initialize the tempArr, I just want to know… does static variables doesn’t initialize themselves if they are empty?

Your public “foo” variable is being initialized in the inspector for you. Static variables don’t show in the inspector regardless of whether they are public or not (they can’t, since “static” means there can only be one instance of that variable), so you have to initialize it yourself. If you made “foo” private rather than public, you’d have the same issue, so it’s nothing related to static variables, it’s about the inspector doing the array initializing.

By the way, never use the JS Array class. If you need a dynamic array, use generic Lists, in which case you can often just leave them as Lists rather than converting back and forth between arrays.

–Eric

This is much better! Thanks again Eric!