Enemy Spawning Limit

I’m working on a 3D game, and an issue I’ve been having is that enemies are spawning too quickly despite a variable that I thought would act as a limit (enemiesToSpawn). Does anyone know why this is happening and how I can fix it? Any help would be appreciated!

Here is my script for the enemy:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Enemy : MonoBehaviour
{

    public GameObject laser;
    public GameObject enemy;
    float RandomXValue;
    public int enemiesKilled = 0;
    Rigidbody enemyRigid;
    int enemiesToSpawn = 5;
    int wave = 1;

    void Start()
    {
        enemyRigid = enemy.GetComponent<Rigidbody>();
        enemyRigid.AddForce(0, 0, -6, ForceMode.VelocityChange);
        RandomXValue = Random.Range(-100.0f, 100.0f);
        enemy.transform.position = new Vector3(RandomXValue, enemy.transform.position.y, enemy.transform.position.z);
    }

    void Update()
    {
        if(enemiesToSpawn == 0 && enemiesKilled == (wave * 3) - wave)
        {
            wave++;
            enemiesToSpawn = (wave * 3) - wave;
        }

        if(enemiesToSpawn!=0)
        {
            float randomEnemyTimer = Random.Range(2.0f, 5.0f);
            Invoke("SpawnEnemy", randomEnemyTimer);
            enemiesToSpawn--;
        }
    }

    void OnTriggerEnter(Collider other)
    {
            if (other.gameObject.tag == "Laser")
            {
                enemiesKilled++;
                ResetEnemy();
                laser.gameObject.GetComponent<Shoot1>().ResetLaser();
            }
       
    }

    public void ResetEnemy()
    {
        RandomXValue = Random.Range(-100.0f, 100.0f);
        Instantiate(enemy, new Vector3(RandomXValue, enemy.transform.position.y, 90), Quaternion.identity);
        enemy.gameObject.SetActive(false);
        enemyRigid.AddForce(0, 0, -2, ForceMode.VelocityChange);
    }

    public void SpawnEnemy()
    {
        RandomXValue = Random.Range(-100.0f, 100.0f);
        Instantiate(enemy, new Vector3(RandomXValue, enemy.transform.position.y, 90), Quaternion.identity);
    }

}

private void Update()
{
if(Input.GetKeyDown(KeyCode.P))
startWaves = true;

if(canWawe & startWaves)
StartCoroutine(countdownMethod());

 private void Update()
    {
        if(Input.GetKeyDown(KeyCode.P))
            startWaves = true;
           
            if(canWawe & startWaves)
                StartCoroutine(countdownMethod());
    }

    IEnumerator countdownMethod()
    {
        if(canWawe)
        {
            if (countdown <= 0)
            {
                canWawe = false;
                yield return new WaitUntil(() => canSpawn==true);
                canSpawn = false;
                StartCoroutine(spawnWawe());
                countdown = timeBetwenWawes;
            }else
                countdown -= Time.deltaTime;
        }
    }

    IEnumerator spawnWawe ()
    {
        waveIndex++;
       
        for (int i = 0; i < (enemiesToSpawn * (waveIndex-1)) + 1; i++)
        {
            SpawnEnemy();
            yield return new WaitForSeconds(timeBetwenSpawns);
        }
        canWawe = true;
        canSpawn = true;
        enemiesToSpawn++;
    }

}

Why does each enemy keep track of how many enemies to spawn? That doesn’t seem right.

Usually the enemy spawning mechanism would know how many and how to make an enemy.

The enemy itself doesn’t care.

1 Like

I think I see what you’re saying. When the enemy is Instantiated, it copies over the whole spawn script resulting in every enemy spawning more enemies. I’ll try to put the spawn mechanics into a generic script attached to the EventSystem. Thank you!

Thank you for your response, but as Kurt-Dekker said, the issue was that I attached the spawn mechanics to the enemy, and when the enemy was Instantiated the script was as well, causing every enemy to spawn more enemies.

1 Like