Static Array - Throwing NullReference

I have a static array, and every time I check the length of it - It throws a NullReferenceException. But when I don’t make it static - it works fine.

I need this array to be static as it’s used to hold player data throughout scenes as a reference holder.

Any ideas why static/non static is affecting the length?

I’m literally just doing:

static var myArray : String[];
print(myArray.length);
//prints Null Reference

var myArray : String[];
print(myArray.length);
//prints the actual size ... which is zero at this time

Thanks.

This is most likely a component on a GameObject right?
Unity initializes public non-static variables automatically to be able to use them in the inspector. While the Static variable is not initialized.

You get a NullReferenceException because the static array is not initialized and has no value (is null). Just initialize the array before using it and you should be good to go.