- Home /
New 4.6 UI: Need to know sender of a button click event
In the editor, I can bind a script function to a new UI Button's onClick event, but when it is called, no information is passed about which object was clicked. If I have a dynamic list of buttons and they all use the same event handler, I won't know which one was clicked.
I see some BaseEventData has a selectedObject property, but I'm not sure if that's what I'm looking for, and I don't get an instance of BaseEventData in an onClick handler anyway.
What's a good way to get this information?
Answer by cs_gwl · Dec 03, 2014 at 08:44 AM
Just a workaround...
you can attach a script to the button such as...
public void OnbtnClick()
{
Text txtBtnClicked= gameObject.GetComponentInChildren<Text>();
Debug.Log("ID of the button clicked is " + txtBtnClicked.text);
}
Add this method to the onclick event of your button
What I really wanted was a built-in way for the button to tell an event listener which object raised the event. Apparently, this doesn't exist, so I'd have to do something roughly similar to your suggestion, which is to add a script to each button that listens to its own onClick event, then raises a custom event that includes all the information I need.
Your answer
![](https://koobas.hobune.stream/wayback/20220613172032im_/https://answers.unity.com/themes/thub/images/avi.jpg)