Find the correct index number for array inside two loops

Hey everyone. I’m trying to make some simple inventory with GUI.Box or something. I’ve made an List of type ‘Item’ wich is a custom class that control every single slot and I need to access each index of this list when I open the inventory. For this, I’m using the following code:

public int rows, columns;

void Start ()
{
    for(int x = 0; x < rows; x++)
    {
        for(int y = 0; y < columns; y++)
        {
            int index = ???
            Inventory[index].inUse = false;
        }  
    }  
}

index = x + (y * columns) used to work but just when is a square-like inventory, like 5x5. Anybody have an idea of an algorithm that can calculate the current index based on x and y and in order to access the List ? Otherway I just receive ‘Argument is out of range’ errors, which makes total sense.

Any help would be appreciated! Thanks from now!

Here’s a image of what the index should looks like(don’t need to start with 1).

125804-Example.jpg125

For each row you’ve passed, you’ll need to add the number of items per row to your index – this happens to equal the number of columns in your grid. This seems to match your existing formula:

index = x + (y * columns);

Take a look at your loops, though. You’re using x to track rows (vertical movement), and y to track columns (horizontal movement). Is that what you wanted?

Remember also that arrays are indexed from zero. If I understand your plans correctly, that box you’ve labelled “1” is Inventory[0]. Seems like you understand that, but it’s good to be sure. :wink:

It’s very confusing when you swap x and y. x is usually always horizontal (from left to right). Here’s how i would go about it:

public int rows = 3;
public int columns = 4;

void Start ()
{
    for(int y = 0; y < rows; y++) {
        for(int x = 0; x < columns; x++) {
            int index = x + (y*columns);
            Inventory[index].inUse = false;
        }
    }
}

This will give you an index from 0 to 11 (12 elements == rows*columns)

The important thing is that the next row starts at the next index where the last column ends. first row: 0 1 2 3 so the second row starts at 4 so all indices have to be offset by 4. 4 is actually the length of 1 row (the column count). So the offset is just the row index multiplied with the column count.

 x + y * columns
 0 + 0 * 4       ==  0
 1 + 0 * 4       ==  1
 2 + 0 * 4       ==  2
 3 + 0 * 4       ==  3
 0 + 1 * 4       ==  4
 1 + 1 * 4       ==  5
 2 + 1 * 4       ==  6
 3 + 1 * 4       ==  7
 0 + 2 * 4       ==  8
 1 + 2 * 4       ==  9
 2 + 2 * 4       == 10
 3 + 2 * 4       == 11

Hello
Its been long time after this post.
But this post helped me with visualising 2D array.

In the similar manner, i want to get the index for 3 for loops.
Could @Bunny83 or @rutter please help me

for(int z=0; z< n; z++)
{
       for(int y = 0; y < rows; y++)
       {
            for(int x = 0; x < columns; x++)
            {
             int index = x + (y*columns);
            Inventory[index].inUse = false;
           }
       }
}