- Home /
The question is answered, right answer was accepted
NullReferenceException weird error
I have a class array that I set its size from the "Awake" function and then use in different functions. The problem is that when I start unity and click on play or build my game to test it, I start getting this error:
"NullReferenceException: Object reference not set to an instance of an object"
Coming from this line:
Slot[i].IsTaken = false;
Which is the first line where I start using my array. I thought I had a problem with my array size so I debugged the array size in the "Update" function and I got the same size that I set in the "Awake" function. The weird thing is that when I click on the game object that holds this script, the errors are gone and it starts working fine. But this is not a solution since there's no way it will work outside the unity editor with this error. Oh, and by the way, I'm using Javascript. Is this a common problem with arrays in unity? How can I fix it?
Answer by _Shockwave · Sep 03, 2013 at 08:19 AM
Provide more information around IsTaken and how you created the array but from what I can guess, it's likely that Slot[i] is returning null.
Setting the size of an array does not initialize it with a value, it only register X amount of space for it in memory. Confirm that Slot[i] is not null and that you are initializing it with a value correctly before calling this line:
Slot[i].IsTaken = false;
Actually, declaring an array does set each index with a default value for the given type. The default values for value types can be found here, and null is the default for all reference types.
Because your question gives a reason why you do not excpect a null reference error and Shockwave answered it (correctly, I'd say), I'll just give you a link to my guide to what a null reference error is and how you can fix it yourself. Read it here.
"Actually, declaring an array does set each index with a default value for the given type. The default values for value types can be found here, and null is the default for all reference types." -- Yes, you're correct. I thought they only occupied space and set to null.
I get what you are saying guys. I think can confirm that Slot[i] is not null since I have debugged the size of the Slot array and "i" is a value co$$anonymous$$g from this loop:
for(var i : int = 0; i < Slot.Length; i++)
So Slot[i] can't be null, am I right? and this how I created my array:
class SlotVars { var Name : String; var IsTaken : boolean = false; } var Slot : SlotVars[];
But again, as I stated in my question the weird thing is that when I select on the game object that holds this script, the error is suddenly gone.
Unless you have a Slot[i] = new yourClassNameHere();
somewhere before accessing Slot[i], the initialized value will be null. You can debug if any element is null by Debug.Log(Slot[i]);
. If that says something other than null and you get the error, this is indeed strange.
Thanks a lot Jamora. Please check Jamora's comment for the correct answer.