Spawing Enemies works but works to good

The problem i am having is that i only want it to spawn a certain amount of enemies if the amount is met then stop spawning the enemies. But if the enemy count is less than the max Amount start spawning again. Here is my code it works really good but the above is a problem any help would be amazing thanks.

PS. I don’t know why this code 101010 button is not working correctly

    var AISpawn : Transform[];
    var allEnemies : GameObject[];
    static var enemiesToSpawn : int;
    static var currentEnemies : int;
    var maxEnemiesInt : int;
    var maxEnemies : boolean = false;
    var wave1 : boolean = true;
    
    function Awake()
    {
    	if(wave1 == true)
    		{	
    			Wave1();
    		}
    }
    
    function Update()
    {
    	if(currentEnemies >= maxEnemiesInt)
    		{
    			maxEnemies = true;
    		}
    }
    
    function Wave1()
    {
    	if(maxEnemies == false)
    	{
    	enemiesToSpawn = 10;
    	for(i=0; i < enemiesToSpawn; i++)
    		{
    			var obj : GameObject = allEnemies[Random.Range(0, allEnemies.length-1)]; // Randomize the different enemies to instantiate.
          		var pos: Transform = AISpawn[Random.Range(0, AISpawn.length-1)];  // Randomize the spawnPoints to instantiate enemy at next.
          		Instantiate(obj, pos.position, pos.rotation); 
          		yield WaitForSeconds(2);
          		currentEnemies +=1;
    		}
    	}
    }

I would use another approach: define minEnemies and maxEnemies, and let the coroutine start spawning enemies until maxEnemies is reached. It then stops, and only restart spawning when the number of enemies fall below minEnemies. For this to work, every enemy must decrement currentEnemies before going to hell, or the coroutine will never know when to restart. You can use Start to do the whole thing, since it’s allowed to be a coroutine. The yield WaitForSeconds(2) instruction at the end of the loop gives a 2 seconds pace: each 2 seconds it checks the enemy number and spawns one of them if necessary. Finally: you should use Random.Range(0, array.length), since this function goes to max-1 in the integer version (length-1 makes the last array element never be used)

    var AISpawn : Transform[];
    var allEnemies : GameObject[];
    static var currentEnemies : int = 0;
    var maxEnemies : int = 15;
    var minEnemies : int = 10;
    var spawn = false;
    
    function Start(){
    	while (true){
            if (currentEnemies < minEnemies) 
                spawn = true; // if minEnemies reached start spawning
            if (currentEnemies >= maxEnemies) 
                spawn = false; // if maxEnemies reached stop spawning
            if (spawn){ // if spawning enabled create new enemy
                // Randomize the different enemies to instantiate.
                var obj : GameObject = allEnemies[Random.Range(0, allEnemies.length)];
                // Randomize the spawnPoints to instantiate enemy at next. 
                var pos: Transform = AISpawn[Random.Range(0, AISpawn.length)];  
                Instantiate(obj, pos.position, pos.rotation); 
                currentEnemies +=1;
            }
            yield WaitForSeconds(2); // free Unity for 2 seconds each loop anyway
    	}
    }

It seems you need to place a while loop like so inside of the wave function to hold all your other code.

while(playerisinthearea or something like that.)
{
if(#ofenemiesislessthaniwant)
{
//create enemies
}
else
{
//return to do nothing
}

}