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. Dismiss Notice

Timer Between Waves not counting down

Discussion in 'Scripting' started by brashadam85, Jun 1, 2021.

  1. brashadam85

    brashadam85

    Joined:
    Jan 27, 2021
    Posts:
    14
    I'm trying to make a simple countdown timer in between waves in a wave shooter. The timer appears after each wave a disappears when the next wave starts. The problem is the text never counts down, probably missing something really silly but I've tried so many variations on the following script and I'm a bit lost. Any help would be appreciated.

    I've watched a few videos but cant find anything for making a timer appear and countdown.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SpawnManager : MonoBehaviour
    7. {
    8.     public static SpawnManager instance;
    9.  
    10.     public GameObject[] enemyPrefabs;
    11.     public int enemyIndex;
    12.     public float startDelay = 2;
    13.     public float spawnInterval = 1.5f;
    14.     public float spawnRangeX = 10;
    15.     public float spawnRangeZ = 10;
    16.  
    17.  
    18.     public int waveNumber = 0;
    19.     public int enemySpawnAmount = 0;
    20.     public int enemiesKilled = 0;
    21.     public int enemiesRemaining = 0;
    22.     public Text enemiesLeftText, waveNumberText, nextWaveInText;
    23.     public GameObject remainingEnemies, nextWaveIn;
    24.     public float timeBetweenWaves = 5.00f;
    25.  
    26.     private void Awake()
    27.     {
    28.         instance = this;
    29.     }
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.  
    35.         StartWave();
    36.         enemiesRemaining = enemySpawnAmount;
    37.  
    38.         remainingEnemies.gameObject.SetActive(false);
    39.         nextWaveIn.gameObject.SetActive(false);
    40.  
    41.     }
    42.  
    43.     // Update is called once per frame
    44.     public void Update()
    45.     {
    46.  
    47.  
    48.  
    49.         if (enemiesRemaining <= 0)
    50.         {
    51.             nextWaveIn.gameObject.SetActive(true);
    52.             TimeUntilNextWave();
    53.  
    54.             NextWave();
    55.  
    56.             enemiesRemaining = enemySpawnAmount;
    57.             remainingEnemies.gameObject.SetActive(false);
    58.            
    59.         }
    60.  
    61.     }
    62.  
    63.     public IEnumerator SpawnEnemy()
    64.     {
    65.        
    66.         yield return new WaitForSeconds(timeBetweenWaves);
    67.         nextWaveIn.gameObject.SetActive(false);
    68.  
    69.         Vector3 spawnpos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnRangeZ);
    70.         int enemyIndex = Random.Range(0, enemyPrefabs.Length);
    71.         Instantiate(enemyPrefabs[enemyIndex], spawnpos, enemyPrefabs[enemyIndex].transform.rotation);
    72.  
    73.         enemiesLeftText.text = "Enemies Remaining: " + enemySpawnAmount.ToString();
    74.  
    75.         waveNumberText.text = "Wave " + waveNumber.ToString();
    76.  
    77.     }
    78.  
    79.     public void StartWave()
    80.     {
    81.  
    82.         waveNumber = 1;
    83.         enemySpawnAmount = 6;
    84.         enemiesKilled = 0;
    85.  
    86.         for (int i = 0; i < enemySpawnAmount; i++)
    87.         {
    88.             StartCoroutine(SpawnEnemy());
    89.         }
    90.     }
    91.  
    92.     public void NextWave()
    93.     {
    94.        
    95.         waveNumber++;
    96.         enemySpawnAmount += Random.Range(2, 6);
    97.         enemiesKilled = 0;
    98.  
    99.  
    100.         for (int i = 0; i < enemySpawnAmount; i++)
    101.         {
    102.  
    103.             StartCoroutine(SpawnEnemy());
    104.         }
    105.     }
    106.  
    107.     public void RemainingEnemies()
    108.     {
    109.         enemiesLeftText.text = "Enemies Remaining: " + enemiesRemaining.ToString();
    110.  
    111.         if (enemiesRemaining <= 5)
    112.         {
    113.             remainingEnemies.gameObject.SetActive(true);
    114.  
    115.         }
    116.  
    117.     }
    118.  
    119.     public void TimeUntilNextWave()
    120.     {
    121.         if (timeBetweenWaves > 0)
    122.         {
    123.             timeBetweenWaves -= 1 * Time.deltaTime;
    124.             nextWaveInText.text = "Next Wave In: " + timeBetweenWaves.ToString("0");
    125.         }
    126.  
    127.     }
    128.  
    129. }
     
  2. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    your "timeBetweenWaves" variable is never being reset to 5. so once it gets to 0 it will stay there.

    In TimeUntilNextWave() you should use a different variable to store to "timeTillNextWave" because you're using timeBetweenWaves for different purposes.

    There may also be some other variables that need to be reset between each wave.

    Remember to use "Debug.Log" and breakpoints to help you debug your code and figure out what path it's taking.
     
  3. brashadam85

    brashadam85

    Joined:
    Jan 27, 2021
    Posts:
    14
    Thanks, I knew it would be something minor I was over looking.

    Here's the working code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SpawnManager : MonoBehaviour
    7. {
    8.     public static SpawnManager instance;
    9.  
    10.     public GameObject[] enemyPrefabs;
    11.     public int enemyIndex;
    12.     public float startDelay = 2;
    13.     public float spawnInterval = 1.5f;
    14.     public float spawnRangeX = 10;
    15.     public float spawnRangeZ = 10;
    16.  
    17.  
    18.     public int waveNumber = 0;
    19.     public int enemySpawnAmount = 0;
    20.     public int enemiesKilled = 0;
    21.     public int enemiesRemaining = 0;
    22.     public Text enemiesLeftText, waveNumberText, nextWaveInText;
    23.     public GameObject remainingEnemies, nextWaveIn;
    24.     public float timeBetweenWaves;
    25.  
    26.     private void Awake()
    27.     {
    28.         instance = this;
    29.     }
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.  
    35.         StartWave();
    36.  
    37.         enemiesRemaining = enemySpawnAmount;
    38.         timeBetweenWaves = 0f;
    39.         remainingEnemies.gameObject.SetActive(false);
    40.         nextWaveIn.gameObject.SetActive(false);
    41.  
    42.     }
    43.  
    44.     // Update is called once per frame
    45.     public void Update()
    46.     {
    47.  
    48.         TimeUntilNextWave();
    49.         if(timeBetweenWaves <= 0)
    50.         {
    51.             timeBetweenWaves = 0;
    52.         }
    53.  
    54.         if (enemiesRemaining <= 0)
    55.         {
    56.             timeBetweenWaves = 15f;
    57.            
    58.             nextWaveIn.gameObject.SetActive(true);
    59.  
    60.             NextWave();
    61.            
    62.             enemiesRemaining = enemySpawnAmount;
    63.             remainingEnemies.gameObject.SetActive(false);
    64.            
    65.         }
    66.  
    67.     }
    68.  
    69.     public IEnumerator SpawnEnemy()
    70.     {
    71.  
    72.         yield return new WaitForSeconds(timeBetweenWaves);
    73.  
    74.         timeBetweenWaves = 0f;
    75.         nextWaveIn.gameObject.SetActive(false);
    76.  
    77.         Vector3 spawnpos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnRangeZ);
    78.         int enemyIndex = Random.Range(0, enemyPrefabs.Length);
    79.         Instantiate(enemyPrefabs[enemyIndex], spawnpos, enemyPrefabs[enemyIndex].transform.rotation);
    80.  
    81.         enemiesLeftText.text = "Enemies Remaining: " + enemySpawnAmount.ToString();
    82.  
    83.         waveNumberText.text = "Wave " + waveNumber.ToString();
    84.  
    85.     }
    86.  
    87.  
    88.     public void StartWave()
    89.     {
    90.  
    91.         waveNumber = 1;
    92.         enemySpawnAmount = 6;
    93.         enemiesKilled = 0;
    94.  
    95.         for (int i = 0; i < enemySpawnAmount; i++)
    96.         {
    97.  
    98.              StartCoroutine(SpawnEnemy());
    99.  
    100.         }
    101.     }
    102.  
    103.     public void NextWave()
    104.     {
    105.        
    106.         waveNumber++;
    107.         enemySpawnAmount += Random.Range(2, 6);
    108.         enemiesKilled = 0;
    109.  
    110.  
    111.         for (int i = 0; i < enemySpawnAmount; i++)
    112.         {
    113.  
    114.  
    115.             StartCoroutine(SpawnEnemy());
    116.  
    117.  
    118.         }
    119.     }
    120.  
    121.     public void RemainingEnemies()
    122.     {
    123.         enemiesLeftText.text = "Enemies Remaining: " + enemiesRemaining.ToString();
    124.  
    125.         if (enemiesRemaining <= 5)
    126.         {
    127.             remainingEnemies.gameObject.SetActive(true);
    128.  
    129.         }
    130.  
    131.     }
    132.  
    133.     public void TimeUntilNextWave()
    134.     {
    135.  
    136.             timeBetweenWaves -= 1 * Time.deltaTime;
    137.             nextWaveInText.text = "Next Wave In: " + timeBetweenWaves.ToString("0");
    138.  
    139.  
    140.     }
    141.  
    142. }
     
    Serinx likes this.