Question by
$$anonymous$$ · Apr 19, 2016 at 01:33 PM ·
c#eventsbutton trigger events
How to use Button.onClick?
Hi, I am making a few automatically generated buttons for like a menu for 5 building blocks but I've looked everywhere to find how to use the Button.onClick and couldn't find one working.
for(int i=0; i < 4; i++){
Button buttonTrigger = newButton.GetComponent<Button>();
buttonTrigger.onClick.AddListener(() => UI_ButtonSelect(i));
}
void UI_ButtonSelect(int index){
//select block and hide menu
}
I've found and tried this but the buttons won't work and there were no onClick functions in the inspector.
Comment
Best Answer
Answer by Toon_Werawat · Apr 19, 2016 at 03:06 PM
For some reason. This code was work for me long time ago
buttonTrigger.onClick.AddListener(() => UI_ButtonSelect(i));
But it not work anymore since I'm upgrade to unity 5
But instead. This code was work for me. But it not show on inspector
buttonTrigger.onClick.AddListener(delegate { UI_ButtonSelect(i);});
Your answer
