Add an Object to a List which is part of an array does not work.
I have the problem, that i want to add objects to a list which is part of an array. i was searching in the forum, but could not find a correct solution, because mostly the problem was in the instantiating of the list and that is what i have done correct(as far as i can say):
the error comes in line 27 of the 2nd part of code.
so here is the part of my code that makes the problem:
public class PlaygroundCreatorWorkingConcept : MonoBehaviour
{
public int height;
public int width;
List<WallInformation> WallInformations = new List<WallInformation>();
List<WallInformation>[,] WallInformationSmall;
}
public class WallInformation
{
public Coord coordinate;
public bool isWall;
public float floorHeight;
public float ceilingHeight;
public WallInformation(Coord newCoordinate, bool newIsWall, float newFloorHeight, float newCeilingHeight)
{
coordinate = newCoordinate;
isWall = newIsWall;
floorHeight = newFloorHeight;
ceilingHeight = newCeilingHeight;
}
}
void Awake()
{
WallInformationSmall = new List<WallInformation>[width / 10, height / 10];
for(int i = 0; i >= width / 10; i++)
{
for (int j = 0; j >= height / 10; j++)
{
WallInformationSmall[i, j] = new List<WallInformation>();
}
}
}
this is the top of the programm with all important thing for the question
public void SetWallInformations()
{
for (int x = 0; x < width; x++)
{
for (int z = 0; z < height; z++)
{
WallInformations.Add(new WallInformation(new Coord(x, z), isWall[x, z], floorWorld[x, z], ceilingWorld[x, z]));
}
} //scheint bis hier zu funktionieren, da er bei small 50*50 = 2500 informationen füllt.
foreach (WallInformation WallInformation in WallInformations)
{
Debug.Log(WallInformations.Count);
Debug.Log(WallInformation.coordinate.tileX);
Debug.Log(WallInformation.coordinate.tileZ);
Debug.Log(WallInformation.isWall);
Debug.Log(WallInformation.floorHeight);
Debug.Log(WallInformation.ceilingHeight);
int i = 0;
int j = 0;
if (WallInformation.coordinate.tileX >= i + 0 && WallInformation.coordinate.tileX <= i + 10)
{
Debug.Log("Bin Hier1.");
if (WallInformation.coordinate.tileZ >= j + 0 && WallInformation.coordinate.tileZ <= j + 10)
{
Debug.Log("Bin Hier2.");
WallInformationSmall[i, j].Add(WallInformation);
}
else
{
j++;
//return;
Debug.Log("Bin Hier3.");
}
}
else
{
i++;
j = 0;
//return;
}
i = 0;
Debug.Log("Bin Hier4.");
}
}
the problem is that the line below Debug.Log("Bin Hier2"); does give me this error:
NullReferenceException: Object reference not set to an instance of an object
WallInformationSmall[i, j] is not a list and it is null and cannot add new object
Is this because it is not possible to make an array of lists how I tried at the top, or what am I doing wrong that it is not working as I for my understanding would expect? The thing is when I work with it, it always behaves like I wanted, there is no red underline or something, just when I want to run it it has the error.
ah my mistake. WallInformationSmall[i, j]
is a list but it is just null.
In awake you did the loop wrong.
https://stackoverflow.com/questions/8184306/iterate-through-2-dimensional-array-c-sharp
Fix
for(int i = 0; i < width / 10; i++)
{
for (int j = 0; j < height / 10; j++)
{
WallInformationSmall[i, j] = new List<WallInformation>();
}
}
Sorry, my bad, the loop there was just a mistake I did on my own. I have it fixed a bit ago.
Whatever you are trying pull off with this code is just bad practice in general. Take my advice and reproach your problem and come up with better solution
I tried to get a bit into lists and found this was one of the best solutions for my problem, but made it a bit more complicated for myself that I wanted. But thank you, I think I will try to understand it a bit more and approach it differently. Also I have changed up something and it seems to be better now. I think it's not a nice way to work like that because you get confused yourself with that much indices and to always keep them in $$anonymous$$d.
Answer by Lunoxx · Oct 10, 2018 at 02:57 PM
I think i got some kind of an answer, to my question, or at least i got the solution to the problem i was thinking of.
the problem was to make an array of lists, and how it is working, or add items to it.
and the solution was pretty easy.
List<int>[] myList;
void Start()
{
myList = new List<int>[sizeOfArrayOfLists];
for (int i = 0; int < sizeOfArrayOfLists; i++)
{
myList[i] = new List<int>();
}
}
just wanted to share, if someone has the same problem. if i come across something that does not work with this, I will edit here.