- Home /
 
Coroutine isn't working? (C#)
I want to spawn a wave of meteors, but for some reason my coroutine isn't working and the rocks all spawn at the same time. Can anyone tell me what's going on? Thanks!!
     void WaveStuff (int intensity)
     {
         for (int i=0; i<intensity; i++)
         {
             GameObject temp = Instantiate (Resources.Load("rock1"), new Vector2 (-10,10), Quaternion.identity) as GameObject;
             temp.rigidbody2D.velocity = new Vector2(10,-10);
             StartCoroutine("Wait", .5f);
         }
 
         for (int i=0; i<intensity; i=i+2)
         {
             GameObject temp = Instantiate (Resources.Load("rock2"), new Vector2 (-9,9), Quaternion.identity) as GameObject;
             temp.rigidbody2D.velocity = new Vector2(10,-10);
             StartCoroutine("Wait", 1f);
         }
 
         for (int i=0; i<intensity; i=i+5)
         {
             GameObject temp = Instantiate (Resources.Load("rock3"), new Vector2 (-8,8), Quaternion.identity) as GameObject;
             temp.rigidbody2D.velocity = new Vector2(10,-10);
             Destroy (temp, 4f);
             StartCoroutine("Wait", 2.5f);
         }
     }
 
     IEnumerator Wait(float duration)
     {
         yield return new WaitForSeconds(duration);
     }
 
              Answer by akauper · Apr 13, 2014 at 08:36 PM
You need to return your coroutine and wait for its execution within your main method.
To do this, change your WaveStuff method from 'void' type to 'IEnumerator' type.
Then, call StartCoroutine as, yield return StartCoroutine("Wait", 2.5f);
This is in the Documentation
I've changed your code to reflect this:
     IEnumerator WaveStuff (int intensity)
     {
        for (int i=0; i<intensity; i++)
        {
          GameObject temp = Instantiate (Resources.Load("rock1"), new Vector2 (-10,10), Quaternion.identity) as GameObject;
          temp.rigidbody2D.velocity = new Vector2(10,-10);
          yield return StartCoroutine("Wait", .5f);
        }
  
        for (int i=0; i<intensity; i=i+2)
        {
          GameObject temp = Instantiate (Resources.Load("rock2"), new Vector2 (-9,9), Quaternion.identity) as GameObject;
          temp.rigidbody2D.velocity = new Vector2(10,-10);
          yield return StartCoroutine("Wait", 1f);
        }
  
        for (int i=0; i<intensity; i=i+5)
        {
          GameObject temp = Instantiate (Resources.Load("rock3"), new Vector2 (-8,8), Quaternion.identity) as GameObject;
          temp.rigidbody2D.velocity = new Vector2(10,-10);
          Destroy (temp, 4f);
          yield return StartCoroutine("Wait", 2.5f);
        }
     }
  
     IEnumerator Wait(float duration)
     {
        yield return new WaitForSeconds(duration);
     }
 
              This is working now, but not exactly as I intended. I think I can figure it out now though, so thanks! :)
No problem. If any you need any more help just let me know. Coroutines can be a headache. P$$anonymous$$ me with a bit more background info about what you are trying to do and I'll be happy to help you write your script.
You are most likely looking for the InvokeRepeating method.
Answer by Key_Less · Apr 13, 2014 at 08:47 PM
The yield instruction inside of the coroutine is what stops any code beyond that point from executing until the yield instruction is satisfied. Right now you start the coroutine, it yields for whatever the duration is, and then does nothing afterwards. If you want to delay something from happening, you need to add the code inside of your coroutine, for example:
 IEnumerator Wait(float duration)
 {
    // Code here will execute immediately...
    
    yield return new WaitForSeconds(duration);
 
    // Code here will execute AFTER the coroutine has yielded 
    // the amount of duration.
 }
 
               For your issue specifically, I'd convert your WaveStuff function into a coroutine and change your StartCoroutine calls into yield instructions.
 IEnumerator WaveStuff (int intensity)
     {
        for (int i=0; i<intensity; i++)
        {
          GameObject temp = Instantiate (Resources.Load("rock1"), new Vector2 (-10,10), Quaternion.identity) as GameObject;
          temp.rigidbody2D.velocity = new Vector2(10,-10);
          yield return new WaitForSeconds(0.5f);
        }
 
        // Do the same for your other for loops...
     }
 
               Just remember to call StartCoroutine() to start WaveStuff.
Your answer