GUI - Inventory - For-Loop

Hi everybody,
I’m a beginner in C# and I want to try to write an Inventory.
I tried to use a For-Loop to create the Inventoryslots (GUI.DrawTexture),
because this is not so much to write. But this did not work for me…
How can this be done?

(One more question: When i delete my button… everything disappers… How can i fix this?
Here is my start without for-loops:

using UnityEngine;
using System.Collections;

public class inventory_gui : MonoBehaviour {
	

    Rect windowRect = new Rect(20, 20, 420, 400);

	public Texture2D backDrop;
	
	public float pos_x=10F;
	public float pos_y=50F;
	public float size_x=50F;
	public float size_y=50F;
	
    void OnGUI() {
        windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "Inventory");
    }
 	
	 void DoMyWindow(int windowID) {
		GUI.DragWindow(new Rect(0, 0, 10000, 20));

		
		GUI.DrawTexture(new Rect(pos_x,pos_y,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+50,pos_y,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+100,pos_y,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+150,pos_y,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+200,pos_y,size_x,size_y),backDrop,ScaleMode.StretchToFill);		
		GUI.DrawTexture(new Rect(pos_x+250,pos_y,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+300,pos_y,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+350,pos_y,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		
		GUI.DrawTexture(new Rect(pos_x,pos_y+50,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+50,pos_y+50,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+100,pos_y+50,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+150,pos_y+50,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+200,pos_y+50,size_x,size_y),backDrop,ScaleMode.StretchToFill);		
		GUI.DrawTexture(new Rect(pos_x+250,pos_y+50,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+300,pos_y+50,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		GUI.DrawTexture(new Rect(pos_x+350,pos_y+50,size_x,size_y),backDrop,ScaleMode.StretchToFill);
		
		if (GUILayout.Button(""))
        print("Got a click");
		
	}

Thank you

As an example you may do like this:

void DoMyWindow(int windowID) {
        GUI.DragWindow(new Rect(0, 0, 10000, 20));
		
		for (int dx = 0; dx < 8; dx++)
		{
			GUI.DrawTexture(new Rect(pos_x + dx * 50, pos_y, size_x, size_y), backDrop, ScaleMode.StretchToFill);
		}
		
		for (int dx = 0; dx < 8; dx++)
		{
			GUI.DrawTexture(new Rect(pos_x + dx * 50, pos_y + 50, size_x, size_y), backDrop, ScaleMode.StretchToFill);
		}

        if (GUILayout.Button(""))
        print("Got a click");

    }

But there are a few ways to do the same. Learn C#, loops are not so difficult.

Oh thank you… My mistake :wink:
Shame on me… think I made a mistake in my for loop… something like that:

    void DoMyWindow(int windowID) {
            GUI.DragWindow(new Rect(0, 0, 10000, 20));
           
            for (int dx = 0; dx < 8; dx++)
            {
                GUI.DrawTexture(new Rect(pos_x, pos_y, size_x, size_y), backDrop, ScaleMode.StretchToFill);
                pos_x=pos_x+50;
            }
           
            for (int dx = 0; dx < 8; dx++)
            {
                GUI.DrawTexture(new Rect(pos_x, pos_y, size_x, size_y), backDrop, ScaleMode.StretchToFill);
                pos_y=pos_y+50;
            }
     
            if (GUILayout.Button(""))
            print("Got a click");
     
        }

If I don’t write ( if (GUILayout.Button(“”)) print(“Got a click”):wink: into the method,
nothing will be shown… no box… What’s the reason for it? And how can I solve this? :face_with_spiral_eyes:
I don’t want to have a button :wink: