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:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour
{
public static int enemiesAlive = 0;
public Wave[] waves;
public float timeBetweenWaves;
public Transform spawnpoint;
public Text waveIndexText;
public Text countdownText;
public GameObject countdownPanel;
public GameManager gameManager;
private float countdown = 3.5f;
private int waveIndex = 0;
private void Start()
{
Time.timeScale = 1f;
}
private void Awake()
{
StartCoroutine(CountDown());
this.enabled = true;
}
// Update is called once per frame
void Update()
{
if (enemiesAlive > 0)
{
return;
}
if (waveIndex == waves.Length)
{
gameManager.WinLevel();
this.enabled = false;
}
if (countdown <= 0f)
{
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
return;
}
//countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
waveIndexText.text = "WAVE - " + Mathf.Round(PlayerStats.waves).ToString("00");
}
IEnumerator CountDown()
{
Debug.Log("CountDown Start");
while (countdown > 0)
{
countdownPanel.SetActive(true);
countdown -= Time.deltaTime;
countdownText.text = countdown.ToString("0");
yield return 0;
}
countdownPanel.SetActive(false);
}
//This coroutine is stop working after reloading the scene
IEnumerator SpawnWave()
{
Debug.Log("Spawn Wave");
Time.timeScale = 1f;
PlayerStats.waves++;
Wave wave = waves[waveIndex];
//enemiesAlive = wave.enemyCount;
for (int i = 0; i < wave.enemy1Count; i++)
{
SpawnEnemy1(wave.enemy1);
yield return new WaitForSeconds(1f / wave.enemy1SpawnTime);
}
for (int i = 0; i < wave.enemy2Count; i++)
{
SpawnEnemy2(wave.enemy2);
yield return new WaitForSeconds(1f / wave.enemy2SpawnTime);
}
waveIndex++;
}
void SpawnEnemy1(GameObject enemy1)
{
Debug.Log("Spawn Enemy 1");
Instantiate(enemy1, spawnpoint.position, spawnpoint.rotation);
enemiesAlive++;
}
void SpawnEnemy2(GameObject enemy2)
{
Debug.Log("Spawn Enemy 2");
Instantiate(enemy2, spawnpoint.position, spawnpoint.rotation);
enemiesAlive++;
}
}
Game Manager Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static bool gameIsOver;
public GameObject gameOverUI;
public GameObject completeLevelUI;
private void Start()
{
gameIsOver = false;
Time.timeScale = 1f;
}
// Update is called once per frame
void Update()
{
if (gameIsOver)
{
return;
}
if(PlayerStats.health <= 0)
{
EndGame();
}
}
public void EndGame()
{
Time.timeScale = 0f;
gameOverUI.SetActive(true);
gameIsOver = true;
}
public void WinLevel()
{
gameIsOver = true;
completeLevelUI.SetActive(true);
}
}