- Home /
Question by
DrInternet · Nov 17, 2014 at 11:30 PM ·
coroutine
How to wait for coroutine's end from another class
Hello,
I would like to halt the execution until some coroutine (from another class) is done. So something like:
IEnumerator testo(){
//do stuff
yield return StartCoroutine(someClass.someCoroutine());
//do stuff after someCoroutine is done
}
But obviously compiler won't allow such code. Is there some workaround?
EDIT: typos
Comment
I think using events would be a good solution http://unity3d.com/learn/tutorials/modules/intermediate/scripting/events
But so far I have simply used a variable on other class to see if what I need done is done. Still, I think events would be better
Answer by Addyarb · Nov 18, 2014 at 12:21 AM
Just check if the event is done with a public bool.
using UnityEngine;
using System.Collections;
public class MoveObjectWithButton : MonoBehaviour {
public bool eventIsDone = false;
IEnumerator WaitForSomething(){
eventIsDone = false;
yield return new WaitForSeconds(5);
eventIsDone = true;
}
}
Or, if you just want a bool to trigger a StartCoroutine, try this:
using UnityEngine;
using System.Collections;
public class MoveObjectWithButton : MonoBehaviour {
public bool startEventEnunerator = false;
void Update(){
if (startEventEnunerator) {
StartCoroutine(WaitForSomething());
}
}
IEnumerator WaitForSomething(){
yield return new WaitForSeconds(5);
}
}
Your answer
Follow this Question
Related Questions
problem with coroutines 2 Answers
Increment variable over time 4 Answers
Blinking & resource consumption 1 Answer