Is it possible to start a coroutine using a button from the new UI system (4.6)

I tried Starting a simple coroutine using a button created with the new UI system, but everytime I have a yield in my fuction it stop appearing in the button’s function selector drop down menu in the inspector, the coroutine is a really simple :

while(true) { do something; yield }

Is there something special i need to do or is it just impossible to start a coroutine with a button using the new UI system ?

1 Answer

1

You cannot directly called the coroutine since your method needs to return void but you can just use a wrapper.

public void Wrapper(){
    StartCoroutine(MyCoroutine());
}
private IEnumerator MyCoroutine(){
    yield return null;
}

Simple workaround, works like a charm, thanks !

As a side note you may know but in case, if you wish to prevent multiple calls. private bool isRunning = false; public void Wrapper(){ if(isRunning == false){ StartCoroutine(MyCoroutine()); } } private IEnumerator MyCoroutine(){ isRunning = true; // Do your stuff over multiple frames yield return null; isRunning = false; }

I know that this is old but i would be glad if you can tell me how can i attach a this to a button