user3246203
user3246203

Reputation: 21

How to create Wait/Delay/Pause

I try this:

void RUN()
{
    Debug.Log("Before Corutine");
    StartCoroutine(Test());
    Debug.Log("After Corutine");
}


IEnumerator Test()
{
    Debug.Log("Before Wait");
    yield return new WaitForSeconds(5);
    Debug.Log("After Wait");
}

And I get:

Before Corutine
Before Wait
After Corutine
(after 5 seconds)
After Wait

My dream is get:

Before Corutine
Before Wait
(wait 5 seconds)
After Wait
After Corutine

Is it possible?

Upvotes: 0

Views: 181

Answers (1)

Servy
Servy

Reputation: 203834

You'll need to make RUN a coroutine (adjusting all calls to it accordingly), and you'll need to yield the result of StartCoroutine.

EDIT:

IEnumerator RUN()
{
    Debug.Log("Before Corutine");
    yield return StartCoroutine(Test());
    Debug.Log("After Corutine");
}


IEnumerator Test()
{
    Debug.Log("Before Wait");
    yield return new WaitForSeconds(5);
    Debug.Log("After Wait");
}

Wherever you call RUN(), you must now call with StartCoroutine(RUN());.

Upvotes: 3

Related Questions