- Home /
Putting a limit on the amount of times you can instantiate an Enemy
Hey there,I'm relatively new to unity and I've been trying to create an Enemy Spawner which will spawn enemies over time. I'm trying to create a limit to the number of enemies that can be alive at once but i cant seem to get it working. If anyone can help me that would be awesome.
The code for the Enemy Spawner:
[Header ("Floats")]
public float delay;
public float fastestDelay;
public float mediumDelay;
public static int amountSpawned = 0;
[Header ("Spawning Stuff")]
public int maxAmountToSpawn = 20;
[Space]
public Transform[] spawnPoints;
[Space]
public GameObject[] enemies;
int randomSpawnPoint, randomEnemy;
public bool spawnAllowed;
void Start()
{
amountSpawned = 0;
StartCoroutine(SpawnAnEnemy());
spawnAllowed = true;
}
private void Update()
{
if (amountSpawned <= 19)
{
spawnAllowed = true;
}
}
IEnumerator SpawnAnEnemy()
{
while (true)
{
if (amountSpawned == maxAmountToSpawn)
{
Debug.Log("Max amount has been reached");
spawnAllowed = false;
}
if (Enemy.EnemyDied == true)
{
if(amountSpawned == 0)
{
amountSpawned = 0;
}
amountSpawned--;
}
randomSpawnPoint = Random.Range(0, spawnPoints.Length);
randomEnemy = Random.Range(0, enemies.Length);
Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
amountSpawned++;
Debug.Log(amountSpawned);
yield return new WaitForSeconds(delay);
delay -= 0.1f;//0.5 for example be carefull not make it less than 0
if (delay <= mediumDelay)
{
PlayerController.amountOfPointsToCount = PlayerController.morePointsToCount;
delay -= 0.05f;
}
if (delay <= fastestDelay) delay = fastestDelay;
while (!spawnAllowed)
yield return null;
}
}
}
With what I've made it is working to an extent. As I kill an enemy it takes one off the amount spawned which works great until I get to 20. Once I get to 20 and I kill an enemy it doesn't spawn anymore and I can't work out what's happened. In the Enemy script, I have a public static bool which is triggered when the health is set to 0 and the enemy dies. I thought I could use this to know when an enemy has died to then subtract one from the amount spawned.
What I need to do is make it so it starts spawning again once the amount spawned goes back down from 20. If you could help me out id really appreciate it.
Thank you for your time.
Answer by xxmariofer · Feb 15, 2019 at 10:16 PM
hello, your problem is not understabding how statics fields work, each enemy doesnt have one var called EnemyDied but the class has one, so you are changing all the time 1 var, so probably thats one of the issues, also i dont understand what you are trying to do here
if(amountSpawned == 0)
{
amountSpawned = 0;
}
you are setting amountSpawned to only if its already 0?
also you are using this while as an if
while (!spawnAllowed)
yield return null;
and you are never exiting the loop so is being excuted even if you cant spawn
Sorry, How would you set it so after instantiating 20 enemies you cant instantiate anymore until an enemy is killed. Without the mess I had made here's the script ( when it works properly ):
[Header ("Floats")]
public float delay;
public float fastestDelay;
public float mediumDelay;
[Header("SpawnPoints")]
public Transform[] spawnPoints;
public GameObject[] enemies;
int randomSpawnPoint, randomEnemy;
public bool spawnAllowed;
void Start()
{
StartCoroutine(SpawnAnEnemy());
spawnAllowed = true;
}
IEnumerator SpawnAnEnemy()
{
while (true)
{
while (!spawnAllowed)
yield return null;
randomSpawnPoint = Random.Range(0, spawnPoints.Length);
randomEnemy = Random.Range(0, enemies.Length);
Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
yield return new WaitForSeconds(delay);
delay -= 0.1f; //0.5 for example be carefull not make it less than 0
if (delay <= mediumDelay)
{
// Player Points Stuff Here
delay -= 0.05f;
}
if (delay <= fastestDelay) delay = fastestDelay;
}
}
}
Does that make more sense?
hello, that should be just as easy as a countdown create a var called something like int numberOfEnemies; in the start set it to 0 and behind the instantiate just add add one enemy nomberOfEnemies++; the second while just change it to an if, that is more standart and that should be it, test it and tell me if its giving any errors.
Yup it all works with no errors, I've been trying to figure out a way to do it for a while now and I guess I overcomplicated it. Thank you.