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);
	}

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);
    }

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.