- Home /
Executing an Action after a Coroutine has finished
Hi, I'd like to create a method that takes a function, yields and executes it after the delay. When I define WaitForSecondsThenExecute()
as void it works but doesn't when it's an IEnumerator. Do I need to call the method differently?
public IEnumerator WaitForSecondsThenExecute(Action method, float waitTime)
{
Debug.Log("execute delegate coroutine!");
yield return new WaitForSeconds(waitTime);
method();
}
this is called by:
WaitForSecondsThenExecute(() => test(), 3f);
private void test()
{
Debug.Log("Executed!");
}
thanks
Answer by TonyLi · Aug 16, 2013 at 01:44 PM
Try:
StartCoroutine(WaitForSecondsThenExecute(() => test(), 3f));
my god, this is embarrassing :D
Thank you! I don't think I would've figured it out any time soon. Duh, sometimes it really hurts...
Answer by Jamora · Aug 16, 2013 at 01:58 PM
Your usage of lambda expressions makes me think your actual use case might be slightly more complicated than what you're showing us. However, if this is indeed all you want to achieve. Unity has the Invoke method which does the same thing.
Invoke("test",3f);
I'm aware of Invoke(), thanks for mentioning it. The code I showed is a simplified version of what I'm doing. Thanks for the reply
Your answer
Follow this Question
Related Questions
How to shortcut a lambda expression through a function? 1 Answer
strange behaviour for coroutine and invoke 1 Answer
Inserting a delay 2 Answers
restart level when dead with delay 3 Answers
Executing coroutines consecutively 0 Answers