Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Enemy Spawning Limit

Discussion in 'Scripting' started by arcturus783, Jun 6, 2022.

  1. arcturus783

    arcturus783

    Joined:
    Nov 12, 2021
    Posts:
    55
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Enemy : MonoBehaviour
    7. {
    8.  
    9.     public GameObject laser;
    10.     public GameObject enemy;
    11.     float RandomXValue;
    12.     public int enemiesKilled = 0;
    13.     Rigidbody enemyRigid;
    14.     int enemiesToSpawn = 5;
    15.     int wave = 1;
    16.  
    17.     void Start()
    18.     {
    19.         enemyRigid = enemy.GetComponent<Rigidbody>();
    20.         enemyRigid.AddForce(0, 0, -6, ForceMode.VelocityChange);
    21.         RandomXValue = Random.Range(-100.0f, 100.0f);
    22.         enemy.transform.position = new Vector3(RandomXValue, enemy.transform.position.y, enemy.transform.position.z);
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if(enemiesToSpawn == 0 && enemiesKilled == (wave * 3) - wave)
    28.         {
    29.             wave++;
    30.             enemiesToSpawn = (wave * 3) - wave;
    31.         }
    32.  
    33.         if(enemiesToSpawn!=0)
    34.         {
    35.             float randomEnemyTimer = Random.Range(2.0f, 5.0f);
    36.             Invoke("SpawnEnemy", randomEnemyTimer);
    37.             enemiesToSpawn--;
    38.         }
    39.     }
    40.  
    41.     void OnTriggerEnter(Collider other)
    42.     {
    43.             if (other.gameObject.tag == "Laser")
    44.             {
    45.                 enemiesKilled++;
    46.                 ResetEnemy();
    47.                 laser.gameObject.GetComponent<Shoot1>().ResetLaser();
    48.             }
    49.        
    50.     }
    51.  
    52.     public void ResetEnemy()
    53.     {
    54.         RandomXValue = Random.Range(-100.0f, 100.0f);
    55.         Instantiate(enemy, new Vector3(RandomXValue, enemy.transform.position.y, 90), Quaternion.identity);
    56.         enemy.gameObject.SetActive(false);
    57.         enemyRigid.AddForce(0, 0, -2, ForceMode.VelocityChange);
    58.     }
    59.  
    60.     public void SpawnEnemy()
    61.     {
    62.         RandomXValue = Random.Range(-100.0f, 100.0f);
    63.         Instantiate(enemy, new Vector3(RandomXValue, enemy.transform.position.y, 90), Quaternion.identity);
    64.     }
    65.  
    66. }
     
  2. AnonimiAngels

    AnonimiAngels

    Joined:
    Jan 10, 2020
    Posts:
    4
    private void Update()
    {
    if(Input.GetKeyDown(KeyCode.P))
    startWaves = true;

    if(canWawe & startWaves)
    StartCoroutine(countdownMethod());
    Code (CSharp):
    1.  private void Update()
    2.     {
    3.         if(Input.GetKeyDown(KeyCode.P))
    4.             startWaves = true;
    5.            
    6.             if(canWawe & startWaves)
    7.                 StartCoroutine(countdownMethod());
    8.     }
    9.  
    10.     IEnumerator countdownMethod()
    11.     {
    12.         if(canWawe)
    13.         {
    14.             if (countdown <= 0)
    15.             {
    16.                 canWawe = false;
    17.                 yield return new WaitUntil(() => canSpawn==true);
    18.                 canSpawn = false;
    19.                 StartCoroutine(spawnWawe());
    20.                 countdown = timeBetwenWawes;
    21.             }else
    22.                 countdown -= Time.deltaTime;
    23.         }
    24.     }
    25.  
    26.     IEnumerator spawnWawe ()
    27.     {
    28.         waveIndex++;
    29.        
    30.         for (int i = 0; i < (enemiesToSpawn * (waveIndex-1)) + 1; i++)
    31.         {
    32.             SpawnEnemy();
    33.             yield return new WaitForSeconds(timeBetwenSpawns);
    34.         }
    35.         canWawe = true;
    36.         canSpawn = true;
    37.         enemiesToSpawn++;
    38.     }
    }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    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.
     
    arcturus783 likes this.
  4. arcturus783

    arcturus783

    Joined:
    Nov 12, 2021
    Posts:
    55
    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!
     
  5. arcturus783

    arcturus783

    Joined:
    Nov 12, 2021
    Posts:
    55
    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.
     
    Kurt-Dekker likes this.