- Home /
Question by
jjdoughboy · Aug 31, 2020 at 08:52 PM ·
countdownactivate
how do I activate my countdown
I have 4 levels that have a wave countdown before the enemy will spawn, during the countdown the retry button will actually restart the level but once the countdown is done and enemy has spawned if the retry button is pressed at that point the level scene will reload but the wave countdown won't actually start. I'm not to sure why. cans one help me out. its almost like the retry button is saying that when the countdown isn't active than don't activate the countdown on retry.
here is my function its attached to 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);
}
}
Here is my wave countdown function its attached to my wave spawner 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++;
}
}
Comment
Your answer