I am creating a castle defense game where the enemy regulary spawns (upto limit of 24 ). After the enemy reaches 24, I want to stop creating clones. So far, I have been successful but I want to make sure that if the enemy reaches 23, it starts instantiating upto 24. How?

    public GameObject[] spawnPosition;
    public GameObject enemy;
    private float startTime;
    private float timeForSpawn;
    public float speed;
    public float enemyLimit;
    public List<GameObject> enemyList;
    public bool stopInstantiating;
    void Start () {
        stopInstantiating = false;
        enemyList = new List<GameObject> ();
        enemyLimit = 24f;
        timeForSpawn = startTime;

    void Update () {

        Spawn ();

    void Spawn () {

        timeForSpawn -= Time.deltaTime;
        if (timeForSpawn <= 0 && stopInstantiating == false) {
            timeForSpawn = startTime;
                for (int i = 0; i <= 2; i++) {
                Instantiate (enemy, spawnPosition [i].transform.position, Quaternion.identity);
                if (enemy != null&& stopInstantiating == false) {
                    enemyList.Add (enemy);
                    if (enemyList.Count >= enemyLimit) {
                        stopInstantiating = true;


I see that the list doesn't update itself as I delete the gameObjects in game. How to make sure that list updates and work on that.


There are a couple of ways to approach this problem. You could:

  • Run a function that checks if an item in the list is empty, then remove that item.
  • Use an array instead of a list, then give the Game Object the index number when instantiating. Now you can easily clear the array when the Game Object is destroyed. (This is probably the best and most efficient way.)
  • Don't bother clearing, just check the entire list/array for empty elements when trying to spawn a new Game Object.

Since you know the maximum amount of instances that will be in the list at the same time, you should use an array instead since it's more efficient.

|improve this answer|||||
  • \$\begingroup\$ Hello @basklein . Couild you please write a feasible code for me? \$\endgroup\$ – Sabin Pyakurel Dec 7 '19 at 4:31

How about this:

  • The enemy spawner has a counter of enemies, an int initialized at 0.
  • Every frame it checks if the counter is bigger than some maximum.
  • If not, you spawn an enemy, increment the counter and then, add a listener to the enemy.
  • When the enemy dies, it fires off a method to decrease the counter again?

I can write down some pseudocode for you if you need it.

Edit: Here's what I came up with with. Mind you, this is pseudocode...

public class EnemySpawner{
    int enemyCount = 0;
    int enemyMax = 10;

    public void EventTick(){
        if(enemyCount < enemyMax){

    //the important part goes here
    public void SpawnEnemy(){
        GameObject go = Instantiate(EnemyClass, Location, Rotation);
        //after creating the enemy, you start listening to it's event

//now the tricky part
public class Enemy{
    public Event EnemyKilled;    //this creates something called "an event"

    public void OnDestroyed(){
        EnemyKilled.Invoke();    //this calls it out, everyone listening will react
|improve this answer|||||
  • \$\begingroup\$ Yes. Help me with it. The problem is to get the counter of cloned enemies. \$\endgroup\$ – Sabin Pyakurel Dec 4 '19 at 11:46
  • \$\begingroup\$ It's been a while since I used unity but I will try. \$\endgroup\$ – lilKriT Dec 4 '19 at 16:22
  • \$\begingroup\$ Yes. I'd be glad if you help me. I am waiting. \$\endgroup\$ – Sabin Pyakurel Dec 6 '19 at 12:08
  • \$\begingroup\$ I know I took some time, but I'm in the middle of a gamejam. Also, remember this is pseudocode, you might have to look up some methods name etc. \$\endgroup\$ – lilKriT Dec 8 '19 at 0:05
  • \$\begingroup\$ So, You created two integers in a class that is not derived from monobehaviour. In that class, you created a function spawnEnemy and called in another one EventTick. When the gameObject is cloning, you are adding listener to an event that doesn't exist? Please, could you write a full feasible code for me? \$\endgroup\$ – Sabin Pyakurel Dec 8 '19 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.