The question is answered, right answer was accepted
How can I start a Coroutine with the onClick of a button?
So I just read like 50 different solutions for that Problem and I haven´t found anyone working out for me. So basically I create a button by Instantiating a prefab. After that i want that onClick starts a Coroutine. I tried it by AddListener (like it´s showed in the Documentation):
public void createButtons(JsonProject projectToCreateButtonOf)
{
Button mybutton = Instantiate(prefab);
mybutton.interactable = true;
mybutton.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
StartCoroutine(nameOfCoroutine);
}
It then just tells me that void into UnityAction isn´t possible.
I also tried:
public UnityAction clickEvent;
clickEvent += TaskOnClick();
But that doens´t work out neither.
mybutton.onClick.AddListener(TaskOnClick);
should work. $$anonymous$$ake sure the argument is TaskOnClick
, not TaskOnClick()
(the same if you want to use the clickEvent
: clickEvent += TaskOnClick
Thank you buddy - i´ll work with AddListener(delegate{})
for now. Just realized that this makes things easier than the way i tried to workaround before
Follow this Question
Related Questions
OnClick() event script from a prefab 0 Answers
button.onClick.AddListener in for loop 2 Answers
Problem with enabling menu 0 Answers
What is the C# " public void (name) " equivalent in JS? 1 Answer