- Home /
onClick.AddListener doesn't get assigned to button.
Nothing happens when I try to assign this method to a new button. Can anyone explain why? This worked when I used to do it.
Thanks!
GameObject flag = Instantiate(rivalFlag, rivalBox.transform.position, Quaternion.identity);
flag.transform.SetParent(rivalBox.transform);
flag.GetComponent<Button>().onClick.AddListener(() => OpenCountryPanel(rival));
What do you mean by Nothing happens
?
The listener won't be visible in the inspector, it's the expected behaviour.
If nothing happens when you click on the button, then make sure your function is called, and then, take a look at the answer I gave here and check all the points I've listed.
https://answers.unity.com/questions/1402544/so-my-buttons-stop-working.html
Answer by Cassos · Apr 28, 2020 at 09:25 PM
If it would be always that simple ^^
Button b = flag.GetComponent<Button>();
b.onClick.AddListener(delegate { OpenCountryPanel(rival); });
Greetings,
Max
@Razputin Don't know if it changed or something, but you have to add a delegate to the Listener ^^
It's already what he/she has done with the lambda function.
Thank you for your answer but I believe the lambda functions the same as what you've written. I have also already tried using the delegate as well :(
So what exactly is the problem? It it being called at all? Is the assigment even called at all?
Your answer
Follow this Question
Related Questions
onclick.addlistener only works once 0 Answers
My onclick action listeners I attach to my buttons as I instantiate them only work once 1 Answer
button.onClick.AddListener(method); NOT Working 1 Answer
Weird behaviour when trying to dynamically create buttons 0 Answers
Reference For Button Which Calls OnClick 4 Answers