Yield return doesn't return a local WaitForSeconds variable

Hi, i’m trying to return to a certain point at the end of coroutine with using a local WaitForSeconds variable but it doesn’t return to it. Here’s the code:

private IEnumerator MovingObstacle()
    {
        obsDone = 0;

        WaitForSeconds wait = new WaitForSeconds(1f);
        GameObject curObs = null;
        Transform[] curT = new Transform[2];
        int hv = 0;
        hv = Random.Range(0, 2);
        if(hv == 0)
        {
            curObs = horizontalObs;
            if(hDir % 2 == 0)
            {
                curT[0] = hBasePos;
                curT[1] = hTargetPos;
            }
            else if(hDir % 2 == 1)
            {
                curT[0] = hTargetPos;
                curT[1] = hBasePos;
            }

            hDir += 1;
        }
        else if(hv == 1)
        {
            curObs = verticalObs;
            if(vDir % 2 == 0)
            {
                curT[0] = vBasePos;
                curT[1] = vTargetPos;
            }
            else if(vDir % 2 == 1)
            {
                curT[0] = vTargetPos;
                curT[1] = vBasePos;
            }

            vDir += 1;
        }

        curObs.SetActive(true);

        float timeElapsed = 0f;
        while(timeElapsed < targetTime)
        {
            curObs.transform.position = Vector3.Lerp(curT[0].position, curT[1].position, timeElapsed/targetTime);
            timeElapsed += Time.deltaTime;
            yield return null;
        }

        curObs.SetActive(false);
        curObs.transform.position = curT[0].position;
        obsDone += 1;

        if(obsCount == 0)
        {
            yield break;
        }

        if(obsDone < obsCount)
        {
            print("Return"); //Before you ask, yes i am certain that the program goes here but it doesn't return wait. I checked it with this print function.
            yield return wait;
        }
    }

What am i missing?

What is there to return to here? Right after that yield statement is the end of the coroutine. The coroutine will resume after that yield and then just end, because there is no more code.

1 Like

I still don’t know the exact purpose of using yield return. But i did the same thing whenever i wanted to return to start of the coroutine and it did work.

In a coroutine yield return something; means “Ok I’m going to stop running code here, let the rest of the game continue for a bit, then come back to me.” Since you are yielding a WaitForSeconds(1f), it means “pause here for one second, then continue”. That’s all. It does not jump around the code or anything like that. If you want to control execution of the code you would use all the standard control structures such as:
if else while for etc…

Thanks it’s all clear now. And i realized that i wasn’t jumping into any point with using “yield return something;” i was just using a while loop and it’s repeating itself already, i thought the yield return repeats it. I’m gonna use while to control the coroutine. Thanks for your help

Our very own Bunny83 has also provided a Coroutine Crash Course:

https://answers.unity.com/questions/1749615/coroutines-ienumerator-not-working-as-expected.html?childToView=1749714#answer-1749714

Coroutines in a nutshell:

2 Likes