Destroying random number of enemies

I have placed 15 enemies in my scene. At start of the game I only want to have a random amount of enemies to be in the game, all others have to be removed.

I have defined two variables which hold a minimum (minEnemies) and maximum (maxEnemies) total of enemies in game. At start a random number (randomEnemies) is generated based on the min/max Enemies. If for example the outcome is 3, the script must remove 12 enemies, so 3 are left.

The problem I am experiencing is that there are always more enemies in the game than are allowed. The following 10 numbers are the outcome of running the game 10 times:

Total enemies in game (max = 5):
8, 5, 8, 6, 7, 7, 8, 7, 7, 7.

Who can help me? I’m quite the rookie when it comes to programming.

var minEnemies : int = 1; //have at least 1 enemy in game
var maxEnemies: int = 5; //have no more than 5 enemies in game

function randomizeEnemies (){
	var randomEnemies : int = Random.Range(minEnemies, maxEnemies);
    var allEnemies : GameObject[];
    allEnemies = GameObject.FindGameObjectsWithTag("Enemy");

    if(allEnemies.Length > 0){
    	var removeEnemies = allEnemies.Length - randomEnemies;
    	for (var i=0;i<removeEnemies;i++){
    		var objectToDestroy = allEnemies[Random.Range(0 , allEnemies.length - 1)];
	        if(objectToDestroy.tag == "Enemy")
	        {
	            Destroy(objectToDestroy);
	        }
    	}
    }
}

function Start() {
	randomizeEnemies();
}

This is an alternative implementation that tracks which/when the enemies are successfully destroyed.

function randomizeEnemies (){
    var randomEnemies : int = Random.Range(minEnemies, maxEnemies);
    var allEnemies : GameObject[];
    allEnemies = GameObject.FindGameObjectsWithTag("Enemy");
    if(allEnemies.Length > 0){
        var removeEnemies = allEnemies.Length - randomEnemies;
        //for (var i=0;i<removeEnemies;i++){ // we don't know how many time to loop
        while(removeEnemies > 0) { // but we do know how many items to remove
            var nextToRemove: int  = Random.Range(0 , allEnemies.length); // when using Random.Range with ints it is exclusive of the upper bound
            var objectToDestroy = allEnemies[nextToRemove];
            //if(objectToDestroy.tag == "Enemy") {//FindGameObjectsWithTag will only return objects with this tag
            if(objectToDestroy != null) { // instead check that slot held object                
                Destroy(objectToDestroy);
                allEnemies[nextToRemove] = null; // mark slot as empty
                removeEnemies--; //count this removal as successful  
            }
        }
    }
}

My guess is that you’re randomly trying to remove an enemy that you already removed, which will throw an exception. You could make allEnemies update its list after you Destroy or some conditional codes to make sure there actually is something there to destroy

Try this code for confirmation

try{
  if(allEnemies.Length > 0){
    var removeEnemies = allEnemies.Length - randomEnemies;
    for (var i=0;i<removeEnemies;i++){
      var objectToDestroy = allEnemies[Random.Range(0 , allEnemies.length - 1)];
      if(objectToDestroy.tag == "Enemy")
      {
        Destroy(objectToDestroy);
      }
      //allEnemies = GameObject.FindGameObjectsWithTag("Enemy");
    }
  }
}
catch(e){
  Debug.log("Exception in Destroying enemies");
}