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 /
avatar image
1
Question by Jakeiiii · Nov 04, 2015 at 04:08 PM · eventsystemgui-buttoninstantiatedclicked

Event System: Check which button has been clicked?

So, I'm having the hardest time figuring out how to do something that really should just be in the API.

I'm using the "new" GUI system and I instantiate buttons at run time. When I click a button, I want to find out which instantiated button I have clicked. I can't pass it through in the OnClick() method in the inspector because it hasn't been created yet, I can't set the parameter of the method in the OnClick() method through code (which I really think you should just be able to write something like button.OnClick().PersistentMethod(0).argument = button.gameObject) and I can't seem to get the AddListener() thing working either (and yes, I have used the proper syntax but it's just not working).

So is there any way I can click the button and find out the gameobject that has been clicked? Sounded simple enough in my head.

Comment
Add comment · Show 3
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 Jakeiiii · Nov 04, 2015 at 04:11 PM 0
Share

The buttons are in an array of Buttons (Button[]) so is that part of the issue? I'm trying to, when instantiate them, set the parameter of the method used in OnClick() to be that particular button (button[x].gameObject).

avatar image Cherno · Nov 08, 2015 at 10:53 PM 0
Share

Add a Listener by code and make it call a function that accepts the Buton that was pressed as a parameter.

 button.onClick.AddListener(delegate { ButtonClick(button); });
 
 public void ButtonLeftClick(Button button) {
      Debug.Log(button.name + " was clicked.");
 }
avatar image Jakeiiii Cherno · Nov 09, 2015 at 10:40 AM 0
Share

Answer's been given; and this AddListener Delegate stuff didn't work for the instantiated buttons.

2 Replies

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

Answer by JoshuaMcKenzie · Nov 08, 2015 at 03:12 PM

if you're using the Eventsystem then you also have access to the Event Handlers as well which are super useful. An example script you can have attached to your button prefab that you instaniate:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;


 /// <summary>
 /// Unity Behavior's eventsystem needs a:
 /// 1a) collider and phyics raycaster component on the camera(for 3d gameobject) or
 /// 1b) a graphics raycaster component on the Canvas (for UI)
 /// 2) a monobehavior script (like the one below)
 /// 3) and an eventsystem gameobject (added by default when you create a UI->Canvas, 
 ///     only one can exist in the scene) to work
 /// 
 /// This script simply catches click events related to the object and passes it to where 
 /// you need it to go
 /// </summary>


 public class OnClickRelay : MonoBehaviour,  IPointerClickHandler
 {
     public void OnPointerClick(PointerEventData eventData)
     {
         //passes data to your button controller (or whater you need to know about the
         //button that was pressed), informing that this game object was pressed.
         ButtonController.instance.ButtonPressed(gameobject, eventData);
     }
 }

PointerEventData has tons of useful info about the click event. for example how long you've clicked and if its a double click. you won't always need it, but the interface requires it as a parameter and it never hurts to pass it along to the controller that wants info on the button pressed.

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 Jakeiiii · Nov 08, 2015 at 08:03 PM 0
Share

Okay, brilliant! You've done it.

I was hoping I wouldn't have to use another script but... it gets the job done!

For anyone wondering how exactly to use this script to check which GUI Button you've just pressed, you use the eventData variable that is passed through and get its 'selectedObject' value:

 public void ButtonPressed(PointerEventData ev){
         Debug.Log (ev.selectedObject); //This prints out the button gameobject that was pressed!
     }

I was thinking that just accessing the 'currentSelectedGameObject' from the eventsystem was the same thing but... it turns out it isn't. Problem solved! Thank you SO much, Joshua.

avatar image starikcetin · Nov 08, 2015 at 09:57 PM 0
Share

Nice point. +1

avatar image
1

Answer by starikcetin · Nov 07, 2015 at 07:31 PM

Think reversed. Instead of checking which button is pressed, simply send a parameter to let script know which button called callback. Like:

 public void ButtonPressCallback (int pressedIndex)
 {
     if(pressedIndex == 1)
     {
         Debug.Log("Button 1 pressed");
     }
 }
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 Jakeiiii · Nov 08, 2015 at 10:56 AM 0
Share

But how do I assign the value of the parameter (pressedIndex)? I can't do it through code (as I have explained because there's nothing in the API that allows you to access this value) and I can't do it in the inspector because the buttons haven't been instantiated yet. $$anonymous$$y idea was to pass through the button gameObject rather than a value, but either way I need to know how to assign this parameter to be either.

avatar image starikcetin Jakeiiii · Nov 08, 2015 at 04:01 PM 0
Share

Why are you using OnGUI? Use eventSystem ins$$anonymous$$d. It allows you to pass pirimitive types (int, string etc.) as parameter.

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

35 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 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

Android Buttons Responding Poorly To Touch Controls 2 Answers

How can use OnPointerClick in a generic way? 1 Answer

GUI Button to toggle true false when pressed 1 Answer

button pressed twice / Conversation text skip 5 Answers

GUI problem 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