Inventory - Buttons... problems...

Hello,
I despair of the buttons in Unity… I tried to solve my problem for hours… but without success :frowning:
I tried to comment my script as good as I could and hope that somebody can help me.
I have made many thoughts about my inventory because I have never done this before and now I’m stuck…

I simply don’t get, how to have access on one button. In the manual I did not found something… like an ID for
every button. So that you can say… “Hey button 23423 you get this texture” during the game.
(For example: You are in a game and you got an item and want to put this in your inventory)

############################################
I can open my inventory and every button has no image.
I run through my “sword1” object and it is destroyed…
On the first itemslot is the right picture of this sword.

If i run through the next sword (sword2), the first icon
was replaced with the icon of the new sword.
############################################
How can I fix this?

  1. Here is my inventory with some information about it…

  2. Here is my script (I’m a C# beginner :wink: So don’t get crazy if you see this…) :stuck_out_tongue:

using UnityEngine;
using System.Collections;

public class inventory_gui : MonoBehaviour  {
	
	//Size of Inventory
    Rect windowRect = new Rect(20, 20, 420, 600);
	bool inventory=false;	
	
	public Texture2D back_image;  //Backgroundpicture


	// InventoryItem item;
	public InventoryItem[] items;	
	
	public float pos_x=10F;
	public float pos_y=50F;
	public float size_x=50F;
	public float size_y=50F;
	
	public int itemslots_x=8;
	public int itemslots_y=8;	
 	bool[ ] inventar=new bool[64];

	int[ ] tmp_item_bild=new int[64];
	public int array_value=0;
	
	public bool locker=false;  //Try to lock the gui... hell no..


public void identify_item(string tmpname)
    {		
		/* Is the Slot of the array already in use? If not... do it! */
		if(inventar[array_value]==false)
		{
			Debug.Log("Inventory on Slot  " + array_value + "  was  " + inventar[array_value]);
			inventar[array_value]=true;  /*  the inventory is now in use...  */			
			Debug.Log("Inventory on Slot  " + array_value + "  set to  " + inventar[array_value]);
		}
		/*  Hey, my slot is already in use...  */	
		else
		{
			array_value++; /* use next slot */
			inventar[array_value]=true; /* now the slot is also in use.. */

		}
		
		
		/*  Now I want to identify, which item you got and save the id in the array...  */
		if(tmpname=="sword1")
		{
			tmp_item_bild[array_value]=1;  /* for example: if the name of the object is sword1 --> save it in the array with the value 1 */
			Debug.Log("Sword 1 tmp_item_bild[array_value]=1;  ---->" + tmp_item_bild[array_value]);
			Debug.Log ("Picture:  " + items[tmp_item_bild[array_value]].image);

		}
		else if(tmpname=="sword2")
		{
			tmp_item_bild[array_value]=2;	
			Debug.Log("Sword 2 tmp_item_bild[array_value]=2;  ---->" + tmp_item_bild[array_value]);
			Debug.Log ("Picture:  " + items[tmp_item_bild[array_value]].image);

		}				
    }   
	
	
	void Start()
	{
			//Debug.Log (items[0].leben);

	}
	void Update ()
	{
		/*  Only to open the inventory...  */
		if(Input.GetKeyDown("i")  inventory==false)
		{
			inventory=true;
			Debug.Log("Inventory true");
		}
		else if(Input.GetKeyDown("i")  inventory==true)
		{
			inventory=false;
			Debug.Log("Inventory false");
 		}
	}
	
    void OnGUI()
	{
        if(inventory == true)
		windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "Inventory");
   	}
 	
   
	
	void DoMyWindow(int windowID)
	{
        GUI.DragWindow(new Rect(0, 0, 10000, 20));
        	
		float zeilen=0F;
		
		/*  Two for-loops to create 8 x 8 slots (only the background)..  */
		for (int dy = 0; dy < itemslots_x; dy++)
        {
           for (int dx = 0; dx < itemslots_y; dx++)
           	 {
           	     GUI.DrawTexture(new Rect(pos_x + dx * 50, pos_y+zeilen, size_x, size_y), back_image, ScaleMode.StretchToFill);
           	 }
           	zeilen=zeilen+50;            
        }
		
	
	  zeilen=0F;
	  pos_x=10F;
	  pos_y=50F;
	  size_x=50F;
	  size_y=50F;		
		int i=0;	
		/*  Two for-loops to create buttons for 8 x 8 slots ..  */
		for (int dy = 0; dy < itemslots_x; dy++)			
        {
           for (int dx = 0; dx < itemslots_y; dx++)
           	 {
				//variable i is only a test to lock it.. without it, the hole inventory is full of this item....
				if(inventar[array_value]==true  i==0)  /*  if the slot is in use.. put the picture on this place.. */
				{
				GUI.Button(new Rect(pos_x + dx * 50, pos_y+zeilen, size_x, size_y), items[tmp_item_bild[array_value]].image);  
					i=1;
				}
				else if (inventar[array_value]==false)
				{
           	     GUI.Button(new Rect(pos_x + dx * 50, pos_y+zeilen, size_x, size_y), items[tmp_item_bild[0]].image); /*  in items[...]... is the picture if the slot is empty */
				 //inventar[i]=false;
				}

           	 }
           	zeilen=zeilen+50;            
        }		

     
            if (GUILayout.Button(""))
           Debug.Log("Got a click"); 		
		
        }	
}

I would pass off the actual drawing of the image back to the item itself. So inside InventoryItem class, put a DrawItemGUI() function.

Inside that function put this line

 GUI.Button(new Rect(pos_x + dx * 50, pos_y+zeilen, size_x, size_y), items[tmp_item_bild[array_value]].image);

You will need to pass in the dx and any other variables. Then your loop inside DoMyWindow() looks like this:

 for (int dy = 0; dy < itemslots_x; dy++)            
 {

           for (int dx = 0; dx < itemslots_y; dx++)
            {
                  DrawItemGUI(dx,dy,etc.);
            }

}