For loop looping only once through 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 one, it prints me that error. And my array is certainly completely filled, since I’ve set many Debug.Log() to check its content.

Thanks for your answer.

I think line #37 in your post is an issue?

Your error points to a line that we can’t match up since you shared only partial code (nothing wrong with that, we just can’t match it up). So the only guess we can make is what it says. You are getting an index out of range. You’ll need to see which index you’re trying to access and how you’re getting an index that isn’t there.

1 Like

Indeed, that would be awesome if people told us the line and gave us more info :slight_smile:

I’m fairly sure that in the second outer loop, right after it prints the value, it’s then trying to access mapwidth + 0 as an index (ie: 5) where ‘4’ is the max. :slight_smile:

1 Like

Well, thanks both of you for your answers. I have finally solved my issue with your help, and it’s… Weird. Indeed, it was the mapWidth variable which made the loop impossible. I’ve placed it in my code because I originally had a one-dimensional array first, and I thought I still needed it because Unity crashed each time I tried to print my map whe that variable wasn’t here. But it is fixed now. I feel really dumb for not having realized this sooner.

Thanks, and sorry for bothering you for this issue. By the way, I’ve put a simple sample of my code here to only show you the essential (and mostly because my original code has over 300 lines :confused: ). So I didn’t want to give you more information that would have been useless and would have only confused you. Thanks once again for your help !

Glad you got it solved. A sample is fine, but it helps if you tell us which line the error was pointing to so we have a point of reference. :slight_smile:

Glad it’s fixed, and no bother…