Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

how to add onclick event dynamically in unity

Button button = myButton.getComponent<Button>();
//next, try any of these:
 
//In Unity 5.6 this doesn't work
button.onClick += myMethod;
//Use this instead
button.onClick.AddListener(myMethod);
//Didn't test it
button.onClick.AddListener(delegate{MyMehtod();})
//this will unlikely work
button.onClick.AddListener(() => {MyMethod();});
Source by forum.unity.com #
 
PREVIOUS NEXT
Tagged: #add #onclick #event #dynamically #unity
ADD COMMENT
Topic
Name
1+9 =