Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by dean_dumitru · Feb 24, 2016 at 01:28 AM · guibuttonclickdetectionlayout

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. alt text

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. alt text

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/

example.jpg (19.6 kB)
example2.jpg (18.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges