• 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 Zaffer · Aug 31, 2014 at 07:15 PM · buttonsevents

How to tell which button is pressed

Hi,

I have 216 buttons arranged in a color palette. How can I tell which button has been pressed? Is there an event generated? Can I capture it? Thanks

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

Answer by AyAMrau · Aug 31, 2014 at 07:56 PM

Assuming you are using the new GUI you can add a listener to the onClick action:

  using UnityEngine.UI;

 ...

 // In  Start or other initialisation method:
     myButton.GetComponent<Button>().onClick.AddListener(() => ColorSelected([value]));

...

 private void ColorSelected([argument])
 {
    // do work
 }

Instead of [value] and [argument] insert what you will use to pass the colour value or any other data.

Here's more examples of capturing new GUI events

Comment
Add comment · Show 2 · 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 Zaffer · Aug 31, 2014 at 08:06 PM 0
Share

Hi AyAmrau,

Thanks! That's just what I'm looking for, but I don't think I have the new GUI. I'm using Unity Pro 4.5.3f3, but all it seems to have is the old GUI that runs on function OnGUI(). How do I get the new GUI? Thanks.

avatar image AyAMrau Zaffer · Aug 31, 2014 at 08:10 PM 0
Share

You can download the new version at http://unity3d.com/unity/beta/4.6, however the new GUI is very different from the old OnGUI stuff, so you would have to redo all the work. It's also in beta and therefore not recommended for anything you want to release soon.

So ins$$anonymous$$d if you can show the code you are using now, we can probably come up with how to do that in the version you are using.

avatar image
0
Wiki

Answer by Zaffer · Aug 31, 2014 at 08:22 PM

Thanks again. Here's what I'm trying to do: http://www.bitsong.com/forPosting/Unity/Color%20Cube_Build.html/Color%20Cube_Build.html This is a 3D representation of the RGB color cube. I made it so you can use the mouse and arrow keys to go into it and look around and see how colors relate to each other. I made a palette of 216 colors (partially finished). I would like the user to be able to click on two colors in succession and the cube will then line up along those colors so the user can zoom from one color to the other. You can see this in action if you press the "t" key.

So far I have captured the button presses by putting this code in each button's if clause: "buttonPressed = true;" Then I put the following code in function Start() and function Update(). Unfortunately, it doesn't work right. I get buttonA and buttonB both reading true when I press just one button, and I would like them to be true one at a time. There's something wrong with my logic. Can you help? Thanks.

 function Start(){
     buttonPressed = false;
     buttonA = false;
     buttonB = false;
 }
 function Update(){
     if(buttonPressed == true){
         buttonA = true;
         buttonPressed = false;
     }
     
     if(buttonPressed == false && buttonA == true){
         buttonB = true;
         buttonPressed = false;
     }
     
     Debug.Log("buttonPressed: " + buttonPressed);
     Debug.Log("buttonA: " + buttonA);
     Debug.Log("buttonB: " + buttonB);
     
 }

Think I got it, just needed some conditions: (My apologies for doing my thinking out loud here on Answers)

 function Update(){
     if(buttonA == false && buttonB == false && buttonPressed == true){
         buttonA = true;
         buttonPressed = false;
     }else if(buttonA == true && buttonB == false && buttonPressed == true){
         buttonB = true;
         buttonPressed = false;
         
     }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access the persistent listener gameobjects on a button? 1 Answer

Check if a listener has already been added to a button? 2 Answers

Offsetting RectTransform based on button state button 1 Answer

I want the gameobject field of my persistent listeners to reference an object other than the one they are on. Is this possible? 1 Answer

Any way to pass the function of a listener as a string? 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