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. Dismiss Notice

coroutine isn't waiting

Discussion in 'Scripting' started by C2006_Clarkie, Nov 2, 2020.

  1. C2006_Clarkie

    C2006_Clarkie

    Joined:
    Sep 24, 2020
    Posts:
    6
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         StartCoroutine(Spawn());
    4.     }
    5.  
    6.     IEnumerator Spawn()
    7.     {
    8.         i += 1;
    9.         if (i > 5)
    10.         {
    11.             i = 1;
    12.         }
    13.  
    14.         if (i == 1)
    15.         {
    16.             Instantiate(zombie, new Vector3(spawnonex, spawnoney, spawnonez), Quaternion.identity);
    17.             yield return StartCoroutine(wait());
    18.         }
    19.         else if (i == 2)
    20.         {
    21.             Instantiate(zombie, new Vector3(spawntwox, spawntwoy, spawntwoz), Quaternion.identity);
    22.             yield return StartCoroutine(wait());
    23.         }
    24.         else if (i == 3)
    25.         {
    26.             Instantiate(zombie, new Vector3(spawnthreex, spawnthreey, spawnthreez), Quaternion.identity);
    27.             yield return StartCoroutine(wait());
    28.         }
    29.         else if (i == 4)
    30.         {
    31.             Instantiate(zombie, new Vector3(spawnfourx, spawnfoury, spawnfourz), Quaternion.identity);
    32.             yield return StartCoroutine(wait());
    33.         }
    34.         else if (i == 5)
    35.         {
    36.             Instantiate(zombie, new Vector3(spawnfivex, spawnfivey, spawnfivez), Quaternion.identity);
    37.             yield return StartCoroutine(wait());
    38.         }
    39.         yield return StartCoroutine(wait());
    40.     }  
    41.  
    42.     IEnumerator wait()
    43.     {
    44.         Debug.Log("started wait");
    45.         yield return new WaitForSeconds(2.5f);
    46.         Debug.Log("finsihed wait");
    47.     }
    i have tried many things and searched but whatever I try the zombies always seem to spawn every time the coroutine is called instead of every 2.5 seconds which is what I want (ignore the really bad code there are many better ways to do this I'm just bad if there is a easy way to do so feel free to correct it).
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You're instantiating the zombie BEFORE you wait. So of course the zombie is spawned instantly. Do it in the reverse order. Wait THEN Instantiate.

    Also i'm unsure why you need to wait at the end of the coroutine? That won't do anything really.
     
    Yoreki likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Slow down here. You're starting a fresh coroutine every frame (60 frames per second).

    Take a moment and review how coroutines work before you start trying to chain them all together.

    Start simple with a coroutine that:

    waits
    spawns
    waits
    spawns

    Then see how that works.
     
    Vryken, Yoreki and PraetorBlue like this.
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You yield after Instantiating. So of course it is instantiating right when you start the coroutine. It is instantiating an object, then waiting, then waiting again at the bottom of the coroutine.
    That's what you told it to do ;)
     
    PraetorBlue likes this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    I think my brain automatically replaced Update with Start! Yeah this is bad!
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    In other related news, are you aware you can make Start() into a coroutine "for free?"

    Code (csharp):
    1. IEnumerator Start()
    2. {
    3.   yield return new WaitForSeconds(2.0f);
    4.   Debug.Log( "Yabo!");
    5. }
    It will however stop running permanently if you ever disable the GameObject it is located on, so beware.
     
  7. C2006_Clarkie

    C2006_Clarkie

    Joined:
    Sep 24, 2020
    Posts:
    6
    thanks a lot i just tested it and it waits for the first spawn but once one is spawned it no longer waits.
     
  8. C2006_Clarkie

    C2006_Clarkie

    Joined:
    Sep 24, 2020
    Posts:
    6
    ah ok ill give this ago thanks a lot for the help
     
  9. C2006_Clarkie

    C2006_Clarkie

    Joined:
    Sep 24, 2020
    Posts:
    6
    got it working now thanks a lot, i do see now why it wasn't working before.
     
    Kurt-Dekker likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Excellent! Coroutines are two main things:

    - amazingly easy and awesome ways to do stuff over time

    - extremely easy to confuse yourself with

    I will mark your character sheet down as having reached Level 2 in Coroutine Skill.
     
    JeffDUnity3D likes this.