- Home /
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.
Answer by GameVortex · Dec 12, 2013 at 10:26 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking array fails each time. But change anything and resave and it works until unity closes...... 1 Answer
Array error - Index is less than 0... 3 Answers
[C#] Physics.Raycast returns an array and can't access the length 2 Answers
GameObjects static array NullReferenceException 1 Answer
How to remove null's from 2d array ? It's removing but only 2 out of 4 null's. 0 Answers