I’m trying to alter the unity3d tutorial 2d space shooter project http://unity3d.com/learn/tutorials/projects/space-shooter/spawning-waves to add diffrent waves.
Here is my code (or at least my changes to theirs):
public void Wave1()
{
while (hazardCount > 0)
{
Vector3 spawnPosition = new Vector3 (5,0,30);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (enemy1, spawnPosition, spawnRotation);
Debug.Log("Hazard wave: " + hazardCount);
Wave1Wait();
hazardCount--;
}
wave1 = true;
}
IEnumerator Wave1Wait()
{
yield return new WaitForSeconds(2);
}
I have Wave1() being called in the Update as long as wave1 is false. And have StartCoroutine (Wave1Wait()); in the Start function.
I know that the loop is working because the debug.log is counting down so I’m assume all the Instantiates are just on top of each other so it looks like one to me. So what I want to achieve here is a pause in the While loop.
Thanks again for the help folks. You’ve taught me once before I’m sure you can do it again 
The reason they put the while loop inside the coroutine is so that the rest of the program can continue to run, and the execution is spread out over multiple frames. If you take it outside of the coroutine, it will run all in one frame.
The other problem is that you are calling it incorrectly.
StartCoroutine(Wave1Wait());
This will not work as you expect it to due to the fact that the line
yield return new WaitForSeconds(2);
tells the computer to exit the Wave1Wait() function and do not return to it for 2 seconds. The WaiForSeconds() function will only have an effect on the coroutine and not the rest of the program.
If you want to actually wait for two seconds it would be better to do something like this:
public float timetostart;
public void Start(){
timetostart=Time.time+2;
}
public void Update(){
Wave1();
}
public void Wave1()
{
while (hazardCount > 0)
{
Vector3 spawnPosition = new Vector3 (5,0,30);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (enemy1, spawnPosition, spawnRotation);
Debug.Log("Hazard wave: " + hazardCount);
if(Time.time>timetostart){
hazardCount--;
}
}
wave1 = true;
}
Keep in mind the code above is very basic and will only successfully run once (since we set timetostart in the Start() method). For more information please check the Time.time documentation.
@NA-RA-KU I tried you’re suggestion to but I got the same results.
@Sildaekar
I’ve tried you’re contribution. However it just causes unity to not respond and I have to kill the processes.
Not sure what I’m doing wrong or why this seems harder then it needs to be. But I am thankful for your help.
No, you only wrapped the function call in StartCoroutine, which is only half of what I said.
Which is why you need to put the while loop inside the coroutine, not in the update function. Just look at the example. Their loop isn’t in update.
Should be structured more like:
public bool WaveInProgress = false;
public void Update(){
if (WaveInProgress == false) {StartCoroutine(Wave1Wait());}
}
IEnumerator Wave1Wait()
{
while (hazardCount > 0)
{
WaveInProgress = true;
Vector3 spawnPosition = new Vector3 (5,0,30);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (enemy1, spawnPosition, spawnRotation);
Debug.Log("Hazard wave: " + hazardCount);
yield return new WaitForSeconds(2);
hazardCount--;
}
WaveInProgress = false;
}
@Na-RA-KU
Ah, I miss understood your first post. I think I got it. Changed my script to:
IEnumerator Wave1()
{
while (hazardCount > 0)
{
Vector3 spawnPosition = new Vector3 (5,0,30);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (enemy1, spawnPosition, spawnRotation);
Debug.Log("Hazard wave: " + hazardCount);
hazardCount--;
yield return new WaitForSeconds (spawnWait);
}
wave1 = true;
}
The only issue I’m seeing now is that my spawnWait is set to 1, but the Instantiated enemy1 seems to spawning quite rapidly still. Since spawnWait is 1 shouldn’t there be a 1 second delay before the next Instantiated enemy is spawned?
Thanks again for the help.
Yes, then call that function with
StartCoroutine(Wave1());
Becareful though, you don’t want a bunch of coroutines kicking off and doing the same thing
Use a flag to determine whether it is already running.