Question by
infilrtrator_55 · Apr 07, 2016 at 10:14 AM ·
c#arraynullreferenceexceptionclass
Assigning to array give NullReferenceExecption!?!!
Quite new to c#, I'm trying to make a script that keeps track of some objects player has stored. So, my script has a data class inside it like this:
private class data
{
public int serialNo, coordX, coordY;
private bool stat;
private string type; //Overly simplified
}
I then create an 2D array and initialize it in my start function;
data[,] itemData = new data[sizeX, sizeY];
Now, I want to assign values to array elements so I run nested loops:
for (_x = 0; _x < sizeX; _x++)
{
for (_y = 0; _y < sizeY; _y++)
{
itemData[_x, _y].coordX = someFunc(); //This is where i get the nullreferenceexeption error.
}
}
I know my someFunc() function is ok, I've checked everything else. But I get the following error when I try to assign anything to itemData[,] array. Whats my mistake?
NullReferenceException: Object reference not set to an instance of an object
dataControl.Start () (at Assets/Scripts/dataControl.cs:64)
Thanks in advance;
Comment