Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Yield return doesn't return a local WaitForSeconds variable

Discussion in 'Scripting' started by Y_E_T, Sep 27, 2022.

  1. Y_E_T

    Y_E_T

    Joined:
    Jan 20, 2020
    Posts:
    28
    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:
    Code (CSharp):
    1. private IEnumerator MovingObstacle()
    2.     {
    3.         obsDone = 0;
    4.  
    5.         WaitForSeconds wait = new WaitForSeconds(1f);
    6.         GameObject curObs = null;
    7.         Transform[] curT = new Transform[2];
    8.         int hv = 0;
    9.         hv = Random.Range(0, 2);
    10.         if(hv == 0)
    11.         {
    12.             curObs = horizontalObs;
    13.             if(hDir % 2 == 0)
    14.             {
    15.                 curT[0] = hBasePos;
    16.                 curT[1] = hTargetPos;
    17.             }
    18.             else if(hDir % 2 == 1)
    19.             {
    20.                 curT[0] = hTargetPos;
    21.                 curT[1] = hBasePos;
    22.             }
    23.  
    24.             hDir += 1;
    25.         }
    26.         else if(hv == 1)
    27.         {
    28.             curObs = verticalObs;
    29.             if(vDir % 2 == 0)
    30.             {
    31.                 curT[0] = vBasePos;
    32.                 curT[1] = vTargetPos;
    33.             }
    34.             else if(vDir % 2 == 1)
    35.             {
    36.                 curT[0] = vTargetPos;
    37.                 curT[1] = vBasePos;
    38.             }
    39.  
    40.             vDir += 1;
    41.         }
    42.  
    43.         curObs.SetActive(true);
    44.  
    45.         float timeElapsed = 0f;
    46.         while(timeElapsed < targetTime)
    47.         {
    48.             curObs.transform.position = Vector3.Lerp(curT[0].position, curT[1].position, timeElapsed/targetTime);
    49.             timeElapsed += Time.deltaTime;
    50.             yield return null;
    51.         }
    52.  
    53.         curObs.SetActive(false);
    54.         curObs.transform.position = curT[0].position;
    55.         obsDone += 1;
    56.  
    57.         if(obsCount == 0)
    58.         {
    59.             yield break;
    60.         }
    61.  
    62.         if(obsDone < obsCount)
    63.         {
    64.             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.
    65.             yield return wait;
    66.         }
    67.     }
    What am i missing?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    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.
     
    Bunny83 likes this.
  3. Y_E_T

    Y_E_T

    Joined:
    Jan 20, 2020
    Posts:
    28
    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.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    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...
     
  5. Y_E_T

    Y_E_T

    Joined:
    Jan 20, 2020
    Posts:
    28
    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
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514