Starting coroutine twice?

Hi, rookie question, but In the following code, aren’t we starting coroutine FadeRoutine() twice? First, when we set currentActiveFade = StartCoroutine(FadeRoutine(target, time)); and then again in the next line yield return currentActiveFade;

public IEnumerator Fade(float target, float time)
        {
            if (currentActiveFade != null)
            {
                StopCoroutine(currentActiveFade);
            }
            currentActiveFade = StartCoroutine(FadeRoutine(target, time));
            yield return currentActiveFade;
        }

Nope.

If I’m reading it right (since FadeRoutine code isn’t included), that part of the code stops a fade if it’s currently happening and then…

starts the fade coroutine.

Sure. Thanks for answering.
What does yield return currentActiveFade; do?
Doesn’t that start it again?

I don’t think so. It’s not something that I’ve seen used before, but from my understanding, it’d be the same as yield return null; which basically means, wait a frame and then it would exit the coroutine. I could be wrong.

No, what happens here is that we’re starting the coroutine in the previous line, and assigning it to a variable that points to the coroutine’s handle.

Then, we’re instructing Unity that we’re not processing any more instructions in this coroutine until the coroutine in the handle is finished.

Since it’s a handle to the coroutine, not an invocation, it doesn’t call the coroutine twice, like this would:

yield return FadeRoutine(target, time);

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms