Problem with pause menu/button - It doesn't pause
I'm new to C# and I'm making a game, with a pause menu. I believe time.timeScale is the problem. The button just uses the Toggle function. The pause menu worked before I updated the WaveSpawner script to spawn each wave with different enemies
Pause Menu Script
public GameObject ui;
public string menuSceneName = "MainMenu";
public SceneFader sceneFader;
public Text roundsText;
void Update ()
{
roundsText.text = PlayerStats.Rounds.ToString();
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.P))
{
Toggle();
}
}
public void Toggle()
{
ui.SetActive(!ui.activeSelf);
if (ui.activeSelf)
{
Time.timeScale = 0f;
}
else
{
Time.timeScale = 1f;
}
}
WaveSpawner Script
public static int EnemiesAlive = 0;
public GameOver gameOver;
public Wave[] waves;
public Transform spawnPoint;
public float timeBetweenWaves = 5f; private float countdown = 2f;
public Text waveCountdownText;
private int waveNumber = 0;
void Update () { if (EnemiesAlive > 0) { return; }
if (countdown <= 0f)
{
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
return;
}
countdown -= Time.deltaTime;
countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
waveCountdownText.text = string.Format("{0:00.00}", countdown);
if (gameOver)
{
this.enabled = false;
}
}
IEnumerator SpawnWave () { PlayerStats.Rounds++;
Wave wave = waves[waveNumber];
for (int i = 0; i < wave.count; i++)
{
SpawnEnemy(wave.enemy);
yield return new WaitForSeconds(1f / wave.rate);
}
waveNumber++;
if (waveNumber == waves.Length)
{
Debug.Log("Level Won!");
this.enabled = false;
}
}
void SpawnEnemy(GameObject enemy) { Instantiate(enemy, spawnPoint.position, spawnPoint.rotation); EnemiesAlive++; } }
And what's the problem, exactly? Did you toss in some Debug.Logs to see if the functions are firing? Worth noting that unless the objects in your game either multiply their movements by Time.deltaTime or use FixedUpdate, putting the Time.timeScale to zero isn't going to affect them.
Sorry, I didn't explain it too well. The pause menu paused the game properly before I updated my WaveSpawner to spawn waves with different enemies each wave.
public static int EnemiesAlive = 0;
public GameOver gameOver;
public Wave[] waves;
public Transform spawnPoint;
public float timeBetweenWaves = 5f;
private float countdown = 2f;
public Text waveCountdownText;
private int waveNumber = 0;
void Update ()
{
if (EnemiesAlive > 0)
{
return;
}
if (countdown <= 0f)
{
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
return;
}
countdown -= Time.deltaTime;
countdown = $$anonymous$$athf.Clamp(countdown, 0f, $$anonymous$$athf.Infinity);
waveCountdownText.text = string.Format("{0:00.00}", countdown);
if (gameOver)
{
this.enabled = false;
}
}
IEnumerator SpawnWave ()
{
PlayerStats.Rounds++;
Wave wave = waves[waveNumber];
for (int i = 0; i < wave.count; i++)
{
SpawnEnemy(wave.enemy);
yield return new WaitForSeconds(1f / wave.rate);
}
waveNumber++;
if (waveNumber == waves.Length)
{
Debug.Log("Level Won!");
this.enabled = false;
}
}
void SpawnEnemy(GameObject enemy)
{
Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
EnemiesAlive++;
}
}
I had a look and my enemies move with Time.deltaTime, I'm not sure why it's not working
Your answer
