Hello,
I am callling a coroutine from another script. But caller script must to do something when this coroutine finishes its job. What is the most elegant, modular and efficient design for that purpose.
I can use boolean or interface but they don’t seem like a good design.
I thought broadcast message but I have never used them and I don’t know their efficiency.
You can yield the coroutine from the caller, like so:
// ....
StartCoroutine(FirstClassCaller());
// ...
IEnumerator FirstClassCaller() // Has to be a coroutine itself
{
Debug.Log("Starting.");
yield return OtherClassCoroutine(); // Start the coroutine and wait for it to finish
Debug.Log("Other coroutine just finished.");
}
IEnumerator OtherClassCoroutine()
{
Debug.Log("Waiting.");
yield return new WaitForSeconds(5);
}
If you need something executed after a certain time known by one script but not by the caller, you could just pass the method with it as a callback:
public IEnumerator DoAndExecuteLater(System.Action callback){
yield return new WaitForSeconds(5f);
if (callback != null)
callback();
}
StartCoroutine(DoAndExecuteLater(FunctionToCallLater));