- Home /
The question is answered, right answer was accepted
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 ?
Answer by fafase · Oct 07, 2014 at 05:14 AM
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;
}
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($$anonymous$$yCoroutine());
}
}
private IEnumerator $$anonymous$$yCoroutine(){
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
After you've added a UI Button component to an object, you should go into the button's inspector and drag the gameObject into the button component. After the gameObject is referenced, you can select a method from a dropdown menu. $$anonymous$$ake sure your method is public.