- Home /
How do fix my pause menu
I have a tower defense game I'm working on and have 4 levels with a pause menu that can toggled and within the pause menu I have a retry button that is supposed to reload the level scene and restart the level. the level starts with a enemy wave countdown before the enemy spawns, when the retry button is pressed it will reload the scene but the 1st wave countdown of the level after the retry button was pressed it won't start. I'm not to sure why that is case as all other player interactions work properly, its just the wave countdown that doesn't start.
This is my pause menu script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public GameObject pauseUI;
public GameObject playerPanelUI;
public GameObject shopPanelUI;
public string mainMenu = "MainMenu";
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.P))
{
Toggle();
}
}
public void Toggle()
{
pauseUI.SetActive(!pauseUI.activeSelf);
if(pauseUI.activeSelf)
{
Time.timeScale = 0f;
playerPanelUI.SetActive(false);
shopPanelUI.SetActive(false);
}
else
{
Time.timeScale = 1f;
playerPanelUI.SetActive(true);
shopPanelUI.SetActive(true);
}
}
public void Retry()
{
Toggle();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void Menu()
{
SceneManager.LoadScene(mainMenu);
}
}
This my Wave spawner script my wave countdown is attached to this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour
{
public Transform enemyPrefab;
public Transform spawnPoint;
public float waveInterval = 5f;
public float countdown = 2f;
private int waveIndex = 0;
public Text waveCountdown;
public Wave[] waves;
public GameManager gameManager;
[Header("How Many Enemies Are Left")]
public static int EnemiesAlive;
void Update()
{
if(EnemiesAlive > 0)
{
return;
}
if (countdown <= 0f)
{
// The coroutine will pause execution and automatically
// resume at the next frame
// With coroutines we begin to model behavior without affecting performance
StartCoroutine(SpawnWave());
Debug.Log("Wave Start");
countdown = waveInterval;
return;
}
if (waveIndex == waves.Length)
{
if (EnemiesAlive < 1)
{
this.enabled = false;
gameManager.WinLevel();
}
}
countdown -= Time.deltaTime;
waveCountdown.text = string.Format("{0:00.00}", countdown);
countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
}
/// <summary>
// IENumerator allows for this function to pause before being execute
// in this case we want out waves to spawn incrementally
// On top of this function running incrementally
/// </summary>
/// <returns></returns>
IEnumerator SpawnWave()
{
PlayerStats.Waves++;
Wave wave = waves[waveIndex];
for (int i = 0; i < wave.count; i++)
{
SpawnEnemy(wave.enemy);
// the amount of time the function will pause given the IENumberator
yield return new WaitForSeconds(0.5f);
}
waveIndex++;
}
void SpawnEnemy(GameObject enemy)
{
Instantiate(enemy, spawnPoint.transform.position, spawnPoint.transform.rotation);
EnemiesAlive++;
}
}
Answer by ShySam · Aug 29, 2020 at 12:02 AM
did you try to add an if statement to the count down before it start or when it restarts. insted of having just [ if (countdown <= 0f) ] you can add one more rule simple to it like:
//add this to your countdown script <WaveSpawner >
public bool Start_CountDown;
//and add that rule to if (countdown <= 0f) ... so it will be like:
if (countdown <= 0f && Start_CountDown == true )
//and add this to your enemies secripts
public GameObject Wave_Spawner_Manager; //The object that has WaveSpawner script
void Awake () {
Wave_Spawner_Manager.GetComponent<Wave_Spawner_Manager>().Start_CountDown = ture;
}
tell me if it worked :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613010648im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI Enabled not working? 1 Answer
my wave spawner not working properly 1 Answer
Game stays paused after Restart. 1 Answer
[SOLVED] How can I continue game on UI button press? 1 Answer