Rows and columns of buttons in a for loop

Hello everyone,

For my inventory system, i want to have a grid of 3 x 3 buttons, so a total of 9 buttons. I now have a for loop that counts how many items i have in my inventory array (list) and creates a new button for every item in my inventory array. However, i have no clue how to get multiple buttons next to each other with a Begin- and EndHorizontal inside a for loop. And to be honest, i haven’t use for loops often.

This is a little bit of the code i have:

function OnGUI() {
	if (showInventory) {
		GUILayout.BeginArea(Rect(Screen.width/2 - 350/2, Screen.height/2 - 400/2, 350, 400), inventoryStyle);
			GUILayout.Label("Player inventory");
			for (var i=0; i<playerInventory.Count; i++) {
  •  		GUILayout.EndHorizontal();*
  •  	}*
  •  GUILayout.EndArea();*
  • }*
    I hope it is simple to add more buttons to each row.
    Thanks in advance!

Take GUILayout.BeginHorizontal(); and GUILayout.EndHorizontal(); out of the for loop.