Help fix my Inventory GUI in C#

Hello,

I have a small inventory system that sort of works. Basically, the first line works perfectly, but when it comes to the next ones… It doesn’t anymore. I am using the old GUI system because I don’t really know how to manipulate the new one and I don’t think it really matters. Basically the first item of the second line is fine, but all the ones after it don’t work.

Here’s part of the inventory code (the GUI part):

float width = Screen.width/2-300;
float height = Screen.height/2-175;
//Do stuff only with basic items
int distanceY = 25;
//int lines = Mathf.RoundToInt(items.Count/8);
int currentLine = 0;

for (int i = 0; i < items.Count; i++) {
	int additionalDistance = 15;
	int itemNumber = i;

	if (itemNumber == 8) {
		currentLine += 1;
		itemNumber = 0;
	}

	int distanceX = itemNumber*70;


	distanceY = currentLine * 70;

	distanceX = distanceX + additionalDistance;

	GUI.Button (new Rect(width + distanceX, height + distanceY + 25, 70, 70), icons.ToArray()*)*

}
And here’s a screenshot of how it looks right now:
![alt text][1]
[1]: http://i.imgur.com/50VC95N.png
Thanks!

You should really use the new UI system, it is very simple :slight_smile:
And there are many tutorials available on unity website.

You could maybe use layout groups and layouts for this GUI system (you dont have to do all the calculations to place your items).

This being said, this should be the loop you are looking for :

for (int i = 0; i < items.Count; i++) {
	int additionalDistance = 15;
	
	
	if (i%8 == 0) {
		currentLine ++;
	}
	
	int distanceX = (i%8)*70;
	
	
	distanceY = currentLine * 70;
	
	distanceX += additionalDistance;
	
	GUI.Button (new Rect(width + distanceX, height + distanceY + 25, 70, 70),icons.ToArray() *);*

}
you should also make the number of items in a line (here 8) a parameter.
EDIT :
Ah and the currentLine should be initialized to 1 if I recall.