- Home /
multi dimensional array of lists
I am trying to create a multidimensional array of lists that stores what is on a specific tile on my grid basically the end goal is to get here item = map_data.mapdata[x, y][i]; from what i can gather from other forms posts it should look like this
public class mapdata
{
public List<int>[,] map_Data;
public mapdata(int sizex, int sizey)
{
map_Data = new List<int>[sizex, sizey];
}
}
public mapdata map_data = new mapdata(size_x,size_y);
public DTileMap(int size_x, int size_y)
{
for(int x=0;x<size_x;x++) {
for(int y=0;y<size_y;y++) {
map_data.map_Data[x,y].Add(3);
}
}
}
but it tells me that
NullReferenceException: Object reference not set to an instance of an object DTileMap..ctor (Int32 size_x, Int32 size_y) (at Assets/TileMap_D/DTileMap.cs:107)
Fixed your formatting for you. Its the button up the top with 101010 on it.
The problem as others have indicated it the lack of initialisation. But this data structure looks messy. You should think carefully if this is actually the way you wish to represent your data.
Answer by zharik86 · Nov 01, 2014 at 07:15 AM
I'm not sure, that it's rigth declare:
public List<int>[,] map_Data;
But if works, than you create multi list, but not initialization it. Try:
public mapdata(int sizex, int sizey) {
map_Data = new List<int>[sizex, sizey];
//Initialization each list
for(int i = 0; i < sizex; i++) {
for(int j = 0; j < sizey; j++) {
map_Data[i, j] = new List<int>();
}
}
}
I hope that it will help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170439im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Creating a multidimensional array List 2 Answers
Respawning gameobject not working c# 0 Answers
Copy values between two classes in two lists. 1 Answer
Make Lists within a List 0 Answers