- Home /
Adding single Event listener to multiple ui buttons via scripting
Hi
I need help. I have a scene and in that scene i have around 14-15 btns and i am adding them on the go. I want to add on click event listener to them with argument, but the event listener doesn't take a function with argument.
I don't want to write 14-15 functions for the buttons cause there must be a way around. I also tried using delegates but they also don't work. Please help me out on this. All i need is a way to write on single listener for all buttons and use that according to button click. My function for event listener is of the following form:
public void onbtnClick(int i)
{
...
//value of i will be passed on btn click for id purpose
...
}
Answer by MrSoad · Oct 12, 2014 at 11:05 AM
Hi. I would add this line to the top of your script :
public var iMy_Button_Number : int;
It the inspector window, once this script is on all your buttons, go through and tag each button individually from 0 to 14(by Tag I mean set the value of iMy_Button_Number for each button).
Now when you get the click event pass the var
iMy_Button_Number
Hope this helps
thanks for help. It worked. but i did this with another method for (int i = 0; i < $$anonymous$$AX$$anonymous$$AZE; i++) { level.onClick.AddListener(()=>function(args)); }