grid style inventory giving me "Index out of range" problems

hello, its the first time i ever try to make an inventory and im not sure if this is the best way to make a grid stile inventory, here is the code i used

this script its the one in charge of opening the inventory and as an item database

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Item_DataBase : MonoBehaviour {

	public List <Item> items = new List<Item> ();
	public List <int> invItemsID = new List<int> ();
	public List <GameObject> slots = new List<GameObject>();

	public GameObject panel;
	public Material panelMat;
	public Color panelOnCol;
	public float currColor;
	public bool panelOn;

	public int maxXSprites;
	public int maxYSprites;
	public Vector2 buttonOffset;

	public float spriteCount;

	public GameObject slot;
	float refCol;

	void Start () {
		items.Add(new Item("Iridium", 0, 10, "An element"));
		items.Add(new Item("Uranium", 1, 20, "An other element"));
		items.Add(new Item("Coffe", 2, 30, "Cup o Joe"));
	}
	
	void Update () {
		if (Input.GetKeyDown(KeyCode.I) && panelOn == false) {
			panelOn = true;
			int x = 0;
			int y = 0;

			int currMaxX = 5;
			int currMaxY = invItemsID.Count / 5;

			int itemsLeft = invItemsID.Count;

			while (y < currMaxY){
				while (x < currMaxX){

					if (itemsLeft < 6)currMaxX = x%5; 

					GameObject clone = Instantiate (slot) as GameObject;
					clone.transform.SetParent(panel.transform, false);
					Item_Script iS = clone.GetComponent<Item_Script>();
					iS.myItemIndex = invItemsID[maxXSprites * (x - 1) + y];
					float xPos = -127 + 55 * x;
					float yPos = 90 + buttonOffset.y * y;
					RectTransform rT = clone.GetComponent<RectTransform>();
					rT.localPosition = new Vector3 (xPos, yPos, 0);
					slots.Add(clone);
					itemsLeft --;
					x++;

				}
				y++;
			}
		}
		else panelOn = false;


	
	}

}

and this is the item class

using UnityEngine;
using System.Collections;

public class Item_Data : MonoBehaviour {


}
public class Item{
	
	public string itemName;
	public string itemDescription;
	public int itemID;
	public float itemPrice;
	
	
	public Item (string name, int id, float price, string description){
		
		itemName = name;
		itemDescription = description;
		itemID = id;
		itemPrice = price;
		
	}
}

the line 51 of the first script, where it says “iS.myItemIndex = invItemsID[maxXSprites * (x - 1) + y];” is giving me an index out of range error in the editor. i have revised this script several times and cant find what is wrong. Sorry for the long question, and thanks a lot for your time.

Without reading all the code, I would expect the line to be something like invItemsID[maxXSprites * y + x]; if x and y start at 0.

Assuming, you count x left to right, each row has x cells. You count y top to bottom. So every row is x cells. maxXSprites is (presumably) number of cells in a row, in other words, column count. So maxXSprites * y is how many total cells are in y rows. Then adding x for the current/last row.

Of course, the rest of the code needs the same logic applied. x is counting columns and y is counting rows.

Also see this answer implementing a grid inventory that I have previously answered.