Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Josh707 · Nov 29, 2012 at 01:25 AM · guiinputgridbuttons

Determining GUI button pressed

I have 2 for loops set up creating a grid of GUI buttons according to some variables. All of the buttons are created using one if(GUI.button(...)) line contained in a for loop.

After creating this, I am completely stumped on how to determine which button is being pressed for my inventory functions. Are there ways of assigning IDs to buttons? Or some other way of assigning values?

The way the Unity devs seem to have written the GUI kind of works against what I'm doing as buttons are created as well as checked for input in a single if statement. You can create them without the if statement but I don't know how to check if the button is pressed from there.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eric5h5 · Nov 29, 2012 at 01:37 AM

You can use the index of the loop.

Comment
Add comment · Show 1 · Share
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
avatar image Josh707 · Nov 29, 2012 at 02:08 AM 0
Share

I'm trying to do something like this, have a vote for now

avatar image
1

Answer by BPPHarv · Nov 29, 2012 at 01:39 AM

This is kinda hackish but if you have a local variable that increments inside the for loop each button action can then include this number in it's onclick actions.

 for (var x: int=0;x < numberOfButtons;x++)
      {
      if(GUI.button())
           {
           updateInventory(x);//where x will be the number of this button
           }
      }

If you're using a for each loop where you don't already have a handy index/count variable you just initialize a local variable to 0 before the loop and increment it at the end of the loop. The following is pseudo code.

 var x:int=0;
 for(var in collection)
 {
 if(GUIButton())
   {
   updateInventory(x);
   }
 x++;
 }

Edit : doh beaten by a min by the master himself.. basically this is a more elaborate verson of what eric5h5 posted above.

Comment
Add comment · Share
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

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Activate GUI Button with Enter Key 1 Answer

Dynamically adding (prefab) Buttons to a ScrollView 0 Answers

Can I use GUIStyle and make the button's textures MovieTextures instead of regular 2D Textures? 0 Answers

gui list problem 2 Answers

detect mouseover with grid buttons? 1 Answer


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