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

Resolved Corotine Not Working After Reloading the Scene

Discussion in 'Scripting' started by moneysolutionsxyz, Sep 17, 2022.

  1. moneysolutionsxyz

    moneysolutionsxyz

    Joined:
    Jul 17, 2022
    Posts:
    14
    Hi guys,
    I am working on Tower Defense game. When i run the game, only first time coroutine works. when i reload the Scene (main scene to main menu then main menu to main scene) the coroutine just stop working. First i thought the whole script isn't working then adding some more stuff in same script i realize that script is working fine but only coroutine just stop i don't know why.

    I already check these solutions:
    - Adding Time.timeScale = 1f in Game Manager and in this Script in Start Method.
    - Checking Project Setting/Time (Time Scale is still 1).

    Wave Spawner Script:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Security.Cryptography;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. public class WaveSpawner : MonoBehaviour
    9. {
    10.     public static int enemiesAlive = 0;
    11.     public Wave[] waves;
    12.     public float timeBetweenWaves;
    13.     public Transform spawnpoint;
    14.     public Text waveIndexText;
    15.     public Text countdownText;
    16.     public GameObject countdownPanel;
    17.     public GameManager gameManager;
    18.  
    19.     private float countdown = 3.5f;
    20.     private int waveIndex = 0;
    21.  
    22.     private void Start()
    23.     {
    24.         Time.timeScale = 1f;
    25.     }
    26.     private void Awake()
    27.     {
    28.         StartCoroutine(CountDown());
    29.         this.enabled = true;
    30.     }
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         if (enemiesAlive > 0)
    35.         {
    36.             return;
    37.         }
    38.         if (waveIndex == waves.Length)
    39.         {
    40.             gameManager.WinLevel();
    41.             this.enabled = false;
    42.         }
    43.         if (countdown <= 0f)
    44.         {
    45.             StartCoroutine(SpawnWave());
    46.             countdown = timeBetweenWaves;
    47.             return;
    48.         }
    49.         //countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
    50.         waveIndexText.text = "WAVE - " + Mathf.Round(PlayerStats.waves).ToString("00");
    51.     }
    52.     IEnumerator CountDown()
    53.     {
    54.         Debug.Log("CountDown Start");
    55.         while (countdown > 0)
    56.         {
    57.             countdownPanel.SetActive(true);
    58.             countdown -= Time.deltaTime;
    59.             countdownText.text = countdown.ToString("0");
    60.             yield return 0;
    61.         }
    62.         countdownPanel.SetActive(false);
    63.     }
    64.  
    65.  
    66.     //This coroutine is stop working after reloading the scene
    67.     IEnumerator SpawnWave()
    68.     {
    69.         Debug.Log("Spawn Wave");
    70.         Time.timeScale = 1f;
    71.         PlayerStats.waves++;
    72.         Wave wave = waves[waveIndex];
    73.      
    74.         //enemiesAlive = wave.enemyCount;
    75.         for (int i = 0; i < wave.enemy1Count; i++)
    76.         {
    77.             SpawnEnemy1(wave.enemy1);
    78.             yield return new WaitForSeconds(1f / wave.enemy1SpawnTime);
    79.         }
    80.         for (int i = 0; i < wave.enemy2Count; i++)
    81.         {
    82.             SpawnEnemy2(wave.enemy2);
    83.             yield return new WaitForSeconds(1f / wave.enemy2SpawnTime);
    84.         }
    85.         waveIndex++;
    86.     }
    87.  
    88.  
    89.  
    90.     void SpawnEnemy1(GameObject enemy1)
    91.     {
    92.         Debug.Log("Spawn Enemy 1");
    93.         Instantiate(enemy1, spawnpoint.position, spawnpoint.rotation);
    94.         enemiesAlive++;
    95.     }
    96.     void SpawnEnemy2(GameObject enemy2)
    97.     {
    98.         Debug.Log("Spawn Enemy 2");
    99.         Instantiate(enemy2, spawnpoint.position, spawnpoint.rotation);
    100.         enemiesAlive++;
    101.     }
    102. }
    Game Manager Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     public static bool gameIsOver;
    8.  
    9.     public GameObject gameOverUI;
    10.     public GameObject completeLevelUI;
    11.  
    12.     private void Start()
    13.     {
    14.         gameIsOver = false;
    15.         Time.timeScale = 1f;
    16.     }
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (gameIsOver)
    21.         {
    22.             return;
    23.         }
    24.  
    25.         if(PlayerStats.health <= 0)
    26.         {
    27.             EndGame();
    28.         }
    29.     }
    30.     public void EndGame()
    31.     {
    32.         Time.timeScale = 0f;
    33.         gameOverUI.SetActive(true);
    34.         gameIsOver = true;
    35.  
    36.     }
    37.     public void WinLevel()
    38.     {
    39.         gameIsOver = true;
    40.         completeLevelUI.SetActive(true);
    41.     }
    42. }
     
    Last edited: Sep 17, 2022
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Post scripts using code tags.

    Otherwise, did you try adding a bunch of debug.log calls and see what does and doesn't run? Did you notice any console errors?
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Coroutines live and die along with monobehaviours that started them.

    When you 'reload' a scene, effectively everything gets destroyed and re-instantiated. Any coroutines that were started by these objects will be killed.

    So most likely you will need to restarted the coroutine.
     
  4. moneysolutionsxyz

    moneysolutionsxyz

    Joined:
    Jul 17, 2022
    Posts:
    14
    yes i add some Debug.log and Every functions works fine except one Coroutine (Spawn Wave) in Wave Spawner Script
     
  5. moneysolutionsxyz

    moneysolutionsxyz

    Joined:
    Jul 17, 2022
    Posts:
    14
    i have more then one coroutine in Wave Spawner Script and after reloading the scene every corounine works fine excepts one (SpawnWave)
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Why do you do
    this.enabled = true;
    immediately after? Awake doesn't run unless the object is enabled, so it doesn't make any sense to call that. If your object is disabled, it's not going to start your coroutine either since it's in Awake.
     
  7. moneysolutionsxyz

    moneysolutionsxyz

    Joined:
    Jul 17, 2022
    Posts:
    14
    so i have to move this line of code in start method?
    i have to disable this script after completing the level / winning the game and enable it when restart the level or next level

    or what is the best solution to not spawn enemies when you won or loss the game/level
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    The thing is, Awake doesn't run if the object were disabled at the start of the scene. So, calling enable in Awake from a disabled GameObject doesn't make sense. There is no reason to call enable at all since you left the scene and reload it.

    Hearing you say you need to enable it makes me wonder, if this script on a DontDestroyOnLoad gameObject?
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Code (CSharp):
    1. public static int enemiesAlive = 0;
    Probably your issue. Did you debug this value? Since it's static, if you reload the scene, this value may still be whatever it was when you lost. Which is why I said insert a bunch of debug calls.
     
    moneysolutionsxyz likes this.
  10. moneysolutionsxyz

    moneysolutionsxyz

    Joined:
    Jul 17, 2022
    Posts:
    14
    i remove these 2 lines
    this.enabled = true;
    this.enabled = false;

    from the script and still enemies didn't spawn after reloading the scene
     
  11. moneysolutionsxyz

    moneysolutionsxyz

    Joined:
    Jul 17, 2022
    Posts:
    14
    yes enemies still alive after reloading the scene :( but games object(enemies) didn't showing in the scene after reloading the scene
     
  12. moneysolutionsxyz

    moneysolutionsxyz

    Joined:
    Jul 17, 2022
    Posts:
    14
    Thank You i found the issue and exactly this is the issue after reloading the scene enemies still alive.

    so i put this code in Awake Method and Boom:
    enemiesAlive = 0;

    Thank You again
     
    Last edited: Sep 17, 2022