- Home /
Array items null but appear normal in inspector
Hi all, I've been using unity for a few years but I am really new to arrays. I'm sure there's a simple explanation to this, but I am getting a value of null when trying to access any of the items in InvItems after setting invItems to an array of new Items. Here is the code to the inventory class:
public Item[] invItems;
void Start()
{
invItems = new Item[8];
print(invItems[0]);
}
Here is the code for the Item class (in the same script):
public string name;
public int ID;
public Item()
{
name = "nullItem";
ID = -1;
}
When I enter playmode, it prints out Null for any of the items in the array. But I can see that it actually creates the items in the inspector:
Any advice or help is greatly appreciated!
Answer by GrayLightGames · Oct 14, 2019 at 05:31 AM
Hi @StugTheGuy, I'd recommend using the List class instead of arrays, seems a bit more flexible and user friendly. However, my guess is that when you create an array with a size of 8, it creates an array with 8 slots but doesn't actually call your constructor and put items in those slots. The inspector itself is probably doing that to make sure it doesn't throw an exception. Not sure, but your error makes it seem like from your script's perspective, the array is size 8 and empty.
I would try setting Item[0] = new Item() before your print statement and see if that gets you past the error. Hope that helps!
thank you that makes sense, i'll use a for loop to set the items individually. but do you know why it wouldnt call the constructor when creating the list? is it because unity has no way to know which constructor to use?
Honestly, not completely sure... when an array is created, it fills itself with default values. For ints, as an example, the default value is 0. For a class, it's null. It's just how C# and java does it... my best guess is it's the easiest, cheapest way to create the array and not every situation would call for the elements to be filled at creation time. You also made a good point, it's unknown which constructor to use. Or perhaps, it guarantees that the array can be created and doesn't error out on anyone's constructor code. Which means the internal array constructor has to do less error handling. You could certainly write your own function that would initialize an array to a size and then call a constructor for each slot if that would help you. I find most of the time with an array or a list though, you will probably want to set some values in each array element when you create it, so you would probably want to make a better constructor and wouldn't end up using the default null constructor very much anyway. Glad to help, please accept my answer if that solved it.
That makes sense, I'm using my own function that initializes each Item individually now so it does work, but thank you so much for your explanation and help. Happily accepting your answer.
Answer by HappiiGamer · Oct 14, 2019 at 03:05 PM
By default the invItems = new Item[8] creates an array of 8 nulls.
You need to initialize the items first. Loop through the array and set each item to new Item().
I ended up doing almost exactly like that, thank you for the advice :0