,"For" loop looping only once through my array
Yeah, I know, that error has been reported so many times, and I could fix it by myself so many times, that even I wonder if I am just blind or not.
I have an array[ , ] of Transforms, and two "For" loops, each one looping through one dimension of my array. My array of Tranforms collects all objects inside a grid, and depending on their position, places them at the index [gridWidth, gridHeight]. Nothing too complex so far.
I use this array to establish a correspondance with another script containing some characters linked to each prefab (e.g. 'B' for "BlueCube"). Each time it collects the corresponding character, and each time the code loops through my array, it gets the nex character and prints it into a .txt file.
My code is supposed to print a map. It starts by printing the first line (as expected), but it also print the rest of the lines below the first one. Logical, but it is not what I wanted ; the map is printed backwards.
So, I decided to read the last line of my array, THEN to decrement the index of my gridHeight, so that each line of my map is printed in the right order. And here comes the problem :
As you can see, my loops go through the first line correctly. But as soon as it starts looping through the second line, I get an error message. Here is my code (simplified, but the problem remains here) :
private Transform[,] allObjectsInForegroundLayer;
int Height = 5;
int Width = 5;
void Start(){
allObjectsInForegroundLayer = new Transform[gridSize.gridSizeWidth, gridSize.gridSizeHeight];
Place_Stuff_Here();
Start_Looping();
}
void Place_Stuff_Here(){
//Blablabla, I fill the array here. I know the problem doesn't come from here, since my map was actually filled with characters, even though it was printed backwards.
}
void Start_Looping(){
int mapWidth = 0;
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
print(x +", "+ (Height - 1 - y));
if (i == LayerMask.NameToLayer("Foreground") && allObjectsInForegroundLayer.Length > 0)
{
if (allObjectsInForegroundLayer[x + mapWidth, Height - 1 - y].transform.name != "NoneForeground")
caracterToprint = index.ReturnReceivedChar(allObjectsInForegroundLayer[x + mapWidth, Height - 1 - y].transform.name.Replace("(Clone)", null));
else
caracterToprint = index.ReturnReceivedChar(allObjectsInForegroundLayer[x + mapWidth, Height - 1 - y].transform.name);
}
file.Write(caracterToprint);
}
mapWidth += gridSize.gridSizeWidth;
}
For once, I absolutely don't know where that error comes from, I have already managed to print my map backwards. Basically, what I am trying to do is to get the last line of my array (the Height variable), substracting 1 to it so that it remains inferior to the length of my array, then each time it loops through, it substracts Y, so that if I have a Height of 5, the new index to check will be 4, then 3, then 2, etc...
As you could see on my picture, it goes through the 4th index very well, but as soon as it goes through the 3rd, it prints me that error. I am really sure my array is completely filled, since I've set many Debug.Log() to check its content.
Thanks for your answer.