[C#] Wave spawn system, sometimes occasionally spawns too many waves in a short time.

Hey there, I’m making a space invaders type game where the enemies spawn in waves when the amount of enemies on screen goes below a certain number (3). My wave calling method is being called from FixedUpdate and occasionally instead of incrementing my waves by 1 while the enemies are < 3 it runs the method multiple times in a short period and spawns too many waves at once.

This is the code of my WaveControl method

	void WaveControl(){
		int waveCount = WaveAdjuster() + baseSpawnAm;
		int thisWave = currentWave;
//		Debug.Log("Enemies on screen " + enemiesOnScreen + " wave count " + waveCount + " Base spawn " + baseSpawnAm);
		if(enemiesOnScreen < baseSpawnAm){
			if(spawning == true) spawnWave = false;
			if(spawning == false) spawnWave = true;
			if(spawnWave == true){
//				Debug.Log("spawns wave");
				currentWave++;
			
				if(currentWave > thisWave && spawnWave == true && spawning == false){
					if(currentWave <= 6 ){
//						Debug.Log("currentWave " + currentWave);
						int fiftyFifty = Random.Range(0, 2);
						if(fiftyFifty == 0){
							int spawnAmountA = (int)(waveCount * 0.4);
							if(spawnAmountA < 1) spawnAmountA = 1;
							int spawnAmountB = (int)(waveCount * 0.6);
							if(spawnAmountB < 1) spawnAmountB = 1;
//							Debug.Log("spawn amountA " + spawnAmountA);
//							Debug.Log("spawn amountB " + spawnAmountB);
							StartCoroutine(SpawnController(spawnAmountA, "Attack Ship"));
							StartCoroutine(SpawnController(spawnAmountB, "Fighter Ship"));
							spawnWave = false;

						}
						if(fiftyFifty == 1){
							int spawnAmountA = (int)(waveCount * 0.6);
							if(spawnAmountA < 1) spawnAmountA = 1;
							int spawnAmountB = (int)(waveCount * 0.3);
							if(spawnAmountB < 1) spawnAmountB = 1;
//							Debug.Log("spawn amountA " + spawnAmountA);
//							Debug.Log("spawn amountB " + spawnAmountB);
							StartCoroutine(SpawnController(spawnAmountA, "Attack Ship"));
							StartCoroutine(SpawnController(spawnAmountB, "Fighter Ship"));
							spawnWave = false;
						}
					}
					if(currentWave > 6){
						Debug.Log("currentWave " + currentWave);
						int fiftyFifty = Random.Range(0, 2);
						if(fiftyFifty == 0){
							int spawnAmountA = (int)(waveCount * 0.6);
							int spawnAmountB = (int)(waveCount * 0.2);
//							Debug.Log("spawn amountA " + spawnAmountA);
//							Debug.Log("spawn amountB " + spawnAmountB);
							StartCoroutine(SpawnController(spawnAmountA, "Attack Ship"));
							StartCoroutine(SpawnController(spawnAmountB, "Mother Ship"));
							spawnWave = false;
						}
						if(fiftyFifty == 1){
							int spawnAmountA = (int)(waveCount * 0.8);
							int spawnAmountB = (int)(waveCount * 0.1);
//							Debug.Log("spawn amountA " + spawnAmountA);
//							Debug.Log("spawn amountB " + spawnAmountB);
							StartCoroutine(SpawnController(spawnAmountA, "Attack Ship"));
							StartCoroutine(SpawnController(spawnAmountB, "Mother Ship"));
							spawnWave = false;
						}
					}
				}
			}
		}
	}

I was considering running it as a coroutine but I thought it might just end up running the coroutine multiple times and end up with the same problem. Any suggestions you can give for this problem would be greatly appreciated.

The reason multiple waves are spawning is because (in the exectution order of event functions) Coroutines are run after Update, and FixedUpdate is called more frequently than Update.

You can fix multiple waves spawning by running WaveControl from Update or set spawning=true whenever you create a new coroutine.