- Home /
Accessing/Initializing array that's part of a class.
Hello,
I've avoided having to post a question so far, just finding my information by searching, but I've spent way too long trying to figure this out.
I'm making a grid based level, and I'm reading from a text file to populate my array. I made a class that only has one multidimensional array(but it doesn't work with regular arrays either). Then I make an array of this class, so I can store my levels in an array.
I keep getting this error:
"NullReferenceException: Object reference not set to an instance of an object"
Here is the class, declaration of variable, and function it is used.
[System.Serializable]
public class LevelsClass
{
public int[,] Tile;;
}
public class StageScript : MonoBehaviour {
LevelsClass[] Levels;
void CreateLevelArray()
{
Levels = new LevelsClass[1];
Levels[0].Tile = new int[10, 10]; //ERROR HERE
Levels[0].Tile[0,0] = 1;
//Rest of this doesn't really matter, since the top is to test and it is getting an error.
string l = TxtLevels.ToString();
var le = l.Split('!');
//Debug.Log(lv[0]);
int i = 0;
int j = 0;
int h = 0;
var lev = le[0].Split('\n');
var leve = lev[0].Split(',');
int[,] level = new int[0,0];
Levels = new LevelsClass[le.Length];
while (h < le.Length)
{
lev = le[h].Split('\n');
Levels[h].Tile = new int[yMax - 1, xMax - 1];
while(i < yMax - 2)
{
leve = lev[i].Split(',');
while(j < xMax - 2)
{
Levels[h].Tile[i,j] = int.Parse(leve[j]);
}
leve = null;
j = 0;
i++;
}
lev = null;
i = 0;
h++;
}
}
}
I don't know if reading it into the array will work yet, but I can't even initialize the array from the class. Anyone know what I'm doing wrong.
Thank you so much in advance!
Answer by Fornoreason1000 · Nov 22, 2013 at 05:51 PM
public int[,] Tile;;
this line confuses me on why you didn't get an error there. there should be only 1 semicolon
Levels[0].Tile = new int[10, 10]; //ERROR HERE
Levels[0].Tile[0,0] = 1;
there's is actually nothing wrong with the line itself, the compiler is telling you that Tile is null. because you didn't assign the LevelClass[0] (you assigned the array but no its values)
so try this
Levels = new LevelClass[1];
Levels[0] = new LevelsClass(); //need to be assigned
Levels[0].Tile = new int[10, 10]; //ERROR HERE
Levels[0].Tile[0,0] = 1;
its a good habit to initialize everything to avoid these errors, even when your not using those specif values at all. with ints, floats, doubles the compiler tends to initialize them behind the scenes giving them zero. array don't and neither do there values.
some reference on arrays
http://msdn.microsoft.com/en-us/library/vstudio/2yd9wwz4.aspx
You can have as many semicolons as you want, since they indicate the end of a statement, and empty statements are fine. This is perfectly valid code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Not very useful, but valid....
@Eric5h5 well that explains that
@Ramun Flame sorry made some typos in the Answer, what i meant to say was that Levels[0] was null becuase only the array itself was assigned
Thanks so much! I didn't realize you had to initialize each part of the array before assigning. Works great now.
Also, the double semicolon was just an typing/pasting error. It wasn't actually in my code. I was deleting a comment after pasting, and an extra semicolon must have survived. Thanks again!
Your answer
![](https://koobas.hobune.stream/wayback/20220613122514im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array of custom class objects, possible? 1 Answer
Call method on "Start" of a built-in component? 1 Answer
Need help with assigning game objects to variable 2 Answers
Creating 3D levels with text and XML files 2 Answers
Getting an error trying to assign a value to an array of classes element 0 Answers