How to click on a GUILayout.Button that is already created
Hi,
I have been trying for a while to make an Inventory System that will allow me to collect certain items and delete them from my inventory whenever I want by clicking on the name.
My code looks like this:
static public Dictionary<int, string> InventoryNameDictionary = new Dictionary<int, string>()
{
{0, null},
{1, null},
{2, null},
{3, null},
{4, null},
{5, null}
};
static public List<int> dictonaryAmounts = new List<int>()
{
0,
0,
0,
0,
0,
0
};
void OnGUI()
{
inventoryToggle = GUI.Toggle(new Rect(50, 30, 150, 75), inventoryToggle, "Inventory");
if(inventoryToggle)
{
inventoryRect = GUI.Window(0, inventoryRect, inventoryMethod, "Inventory");
}
}
void inventoryMethod(int windowId)
{
GUILayout.BeginArea(new Rect(5, 50, 400, 400));
GUILayout.BeginHorizontal();
GUILayout.Button(InventoryNameDictionary[0], GUILayout.Height(50));
GUILayout.Box(dictonaryAmounts[0].ToString(), GUILayout.Height(50));
GUILayout.Button(InventoryNameDictionary[1], GUILayout.Height(50));
GUILayout.Box(dictonaryAmounts[1].ToString(), GUILayout.Height(50));
GUILayout.Button(InventoryNameDictionary[2], GUILayout.Height(50));
GUILayout.Box(dictonaryAmounts[2].ToString(), GUILayout.Height(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Button(InventoryNameDictionary[3], GUILayout.Height(50));
GUILayout.Box(dictonaryAmounts[3].ToString(), GUILayout.Height(50));
GUILayout.Button(InventoryNameDictionary[4], GUILayout.Height(50));
GUILayout.Box(dictonaryAmounts[4].ToString(), GUILayout.Height(50));
GUILayout.Button(InventoryNameDictionary[5], GUILayout.Height(50));
GUILayout.Box(dictonaryAmounts[5].ToString(), GUILayout.Height(50));
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
As you can see, I created a Dictionary that will map my items to a certain position in the inventory. So in the inventory, there will be one button containing the name/icon of the item just collected and next to it a count of that specific item inside the inventory.
Whenever the user clicks on the name, I want the script to detect which button was clicked and subtract one from the count. The count is held by dictionaryAmount.
I tried to make event triggers, but they are not working right for this example.
I have been searching the web for hours trying to find a solution. Do you have any suggestions? If so, please let me know. Again, the question is if there is a way for me to detect which button was clicked.
PS: I used this as reference for the Inventory System: http://forum.unity3d.com/threads/inventory-tutorials.197096/
Your answer
![](https://koobas.hobune.stream/wayback/20220612065253im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there a way to detect if a button is pressed if it isn't created programatically? 2 Answers
Change Player "Avatar" to a other "Avatar" by a GUI button click? 0 Answers
Using an UI Image as a Mouse Alternative 0 Answers
Toggle Button: hope it toggle once but it toggle many times 1 Answer
How to add Background Images for Vertical GUILayouts in Unity3d Custom Editor Window 0 Answers