Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with WaitForSeconds() inside a Coroutine(), it just doesn't wait

Discussion in 'Scripting' started by Gorkatan, Feb 20, 2016.

  1. Gorkatan

    Gorkatan

    Joined:
    Sep 6, 2015
    Posts:
    12
    Hello,

    I'm trying to figure out how WaitForSeconds works inside a coroutine. As I understand it just delays a fixed amount of seconds the return of a coroutine. So to test it, I wrote a very basic script:

    Code (CSharp):
    1. protected  void Start ()
    2.     {  
    3.  
    4.         MoveItMoveIt();
    5.     }
    6.  
    7. protected void MoveItMoveIt()
    8.     {
    9.         for(int i = 0; i < 5; i++)
    10.         {
    11.             Debug.Log ("Going to Coroutine", gameObject);
    12.  
    13.             StartCoroutine(ColdDown());
    14.  
    15.             Debug.Log ("Coming back from Coroutine", gameObject);
    16.         }
    17.     }
    18.  
    19. private IEnumerator ColdDown()
    20.     {
    21.         yield return new WaitForSeconds(2.0f);
    22.     }

    I was expecting to watch the console display the "Coming back from Courutine" message 2 seconds later than "Going to Coroutine", but instead the console display inmediately the 10 messages of the for loop.

    Can anyone tell me what am I doing wrong? Thank you for your feedback.

    Just add: I'm working with C# and the gameObject that contents this script is instantiated by a gameManager as soon as the "game" starts.
     
    smock_74 likes this.
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Put the loop and your debug messages inside the coroutine.

    The way it's currently set up, you're telling this behaviour to start 5 coroutines all at once, that simple wait for 2 seconds, and then do nothing.
     
    smock_74 and Gorkatan like this.
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're just starting the coroutine, not yielding on it. Note that there's no reason to even have a coroutine that only has WaitForSeconds, just replace the coroutine call with WaitForSeconds.

    --Eric
     
    Gorkatan likes this.
  4. Gorkatan

    Gorkatan

    Joined:
    Sep 6, 2015
    Posts:
    12

    Thanks for your reply. I understand what you mean.

    So my goal was to call the coroutine 5 times, with a 2 second delay between each call. I though the coroutine was like a thread and the yield was some kind of sleep (I'm still very noob :confused:). I will try once again using flags, and post the results.
     
    Last edited: Feb 20, 2016
  5. Gorkatan

    Gorkatan

    Joined:
    Sep 6, 2015
    Posts:
    12
    Thanks for the feedback Eric, I tried what I think you said, but it is still not dealy between messages in the console. Just all of them appear together at the beggining.

    Code (CSharp):
    1. protected override void Start ()
    2.     {
    3.         MoveItMoveIt();
    4.     }
    5.      
    6.  
    7. protected void MoveItMoveIt()
    8.     {
    9.         for(int i = 0; i < 5; i++)
    10.         {
    11.             Debug.Log ("Going to Courutine", gameObject);
    12.             new WaitForSeconds(3.5f);
    13.             Debug.Log ("Comming back from Courutine", gameObject);
    14.         }
    15.     }
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  7. Gorkatan

    Gorkatan

    Joined:
    Sep 6, 2015
    Posts:
    12
    Ok, I got what I wanted. Thank you very much for your comments guys.

    My final goal was to make a gameObject move every X seconds, for example an enemy. Changing the delay of WaitForSeconds I make it move faster or slower depenging on the type of enemy. So here it is what I wrote:

    Code (CSharp):
    1. private void Start ()
    2.      {
    3.         StartCoroutine(ColdDown());
    4.  
    5.      }
    6.  
    7. private void MovingThings()
    8.     {
    9.         Debug.Log ("Moving Enemy", gameObject);
    10.     }
    11.  
    12.     private IEnumerator ColdDown()
    13.     {
    14.         //Assuming the enemy is always moving
    15.         for(;;)
    16.         {
    17.             //Moving every 1.5 seconds
    18.             yield return new WaitForSeconds(1.5f);
    19.             MovingThings();
    20.         }
    21.  
    22.     }
     
  8. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    Hope it can help you

    Code (CSharp):
    1. using System;
    2.  
    3. public void YourFunc()
    4.     {
    5.            StartCoroutine(DelayFunc(()=>
    6.             {
    7.                 for(int i = 0; i < 5; i++)
    8.                 {
    9.                    Debug.Log("HI");
    10.                 }
    11.             }, 1.0f));
    12.     }
    13.  
    14.  
    15.     IEnumerator DelayFunc(Action func, float delay)
    16.     {
    17.         yield return new WaitForSeconds(delay);
    18.         func();
    19.     }
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That won't work at all; the OP already posted working code (that's much more straightforward).

    --Eric
     
  10. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    What do u mean of won't work at all?
    because it works on me?
    it will print hi * 5 after 1 seconsd

    what is the problem?
    ps: just want to learn more things


    edit: i guess what you mean is it isn't print 5 times each with a delay 1 secs
    my bad that i didn't read through his post, sry
     
    Last edited: Feb 22, 2016