Hey, So I’ve made a system that seems to work from my tests. This assumes that you have made a prefab for your enemy and also given the prefab a tag.
Here is the code:
//Spawn Variables
var spawnPoint0 : GameObject; //Add in more or less depending on your amount of spawn locations
var spawnPoint1 : GameObject;
var spawnPoint2 : GameObject;
var spawnPoint3 : GameObject;
var spawnPoint4 : GameObject;
//Enemy Variables
var currentActive : int; //Current amount of active enemies
var enemyPrefab : GameObject; //Your enemy prefab should be dragged into here
var maxNumber : int;
var maxActive : int;
function setMaxNumber(numberMax : int, numberActive : int) //numberMax is the maximum overall, numberActive is the maximum allowed to be active
{
maxNumber = numberMax; //Set variable to change maximum overall
maxActive = numberActive; //Set variable to change maximum active at one time
spawnEnemies();
}
function spawnEnemies ()
{
var randSpawn : int;
if(currentActive < maxActive maxNumber > 0)
{
randSpawn = Random.Range(0, 5); //Change the max number to the number of sapwn locations you have
switch(randSpawn)
{
case 0:
Instantiate(enemyPrefab, spawnPoint0.transform.position, Quaternion.identity);
addActive();
break;
case 1:
Instantiate(enemyPrefab, spawnPoint1.transform.position, Quaternion.identity);
addActive();
break;
case 2:
Instantiate(enemyPrefab, spawnPoint2.transform.position, Quaternion.identity);
addActive();
break;
case 3:
Instantiate(enemyPrefab, spawnPoint3.transform.position, Quaternion.identity);
addActive();
break;
case 4:
Instantiate(enemyPrefab, spawnPoint4.transform.position, Quaternion.identity);
addActive();
break;
//Add in more cases based on your number of spawn points, because 0 is included in random.range your end case should always be 1 less than you max variable
}
}
}
function addActive ()
{
currentActive++;
maxNumber --;
spawnCooldown();
}
function removeActive()//Each time an enemy is killed, call this function
{
currentActive--;
spawnEnemies();
}
function reset()
{
var enemies : GameObject[];
enemies = GameObject.FindGameObjectsWithTag("EnemyTag"); //Replace this with with the tag of your prefab
if(enemies != null) //Check if any enemies exist, if they do destroy them
{
for(var i = 0; i < enemies.length; i++)
{
Destroy(enemies[i].gameObject);
}
}
maxActive = 0; //reset variables for new wave
maxNumber = 0;
currentActive = 0;
}
function spawnCooldown ()//Waits 1 second before spawning another enemy
{
var i : float;
var rate : float;
rate = 2;//Change the rate to increase/decrease
while (i < 1)
{
i+= Time.deltaTime * rate;
yield;
}
spawnEnemies();
}
To use this code you must take the following steps:
- Drag and drop the code onto an object in your scene (The camera is a good choice as it is never removed)
- Change the amount of spawn location variables to the correct number you currently have
- Change the Random.Range maximum value to the maximum spawn locations you have
- Change the tag in the reset function to your enemy tag
- Drag and drop the spawn locations into the variables
- Drag and drop your prefab into the prefab variable
In order to spawn enemies you must call the setMaxNumber function with the maximum overall and maximum active so e.g. setMaxNumber(40, 10);
You only need to call this once at the start of the wave.
In order to remove enemies you must call the removeActive() function upon an enemy death or another enemy will not spawn.
Once all the enemies have been spawned/killed and you are ready to start a new wave you should call the reset() function.
Then you can call the setMaxNumber function again, with your increased difficulty e.g. setMaxNumber(60,20);
P.S. You can change the spawnCooldown function so that enemies you spawned faster or slower, I added this so that enemies would not spawn on top of eachother.
I hope this helps with what you were looking for. 