- Home /
Adding Wave Count to the Asteroid Tutorial?
I am trying to add a wave count system to the Asteroid tutorial game and I have positioned my wave counter (waves++) just after the for loop in the program. However whenever the waves only increment after an asteroid has been destroyed. So if you proceed throughout the wave without killing any asteroids the wave count text wont update.
script-
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour { public GameObject hazard; public Vector3 spawnValues; public int hazardCount; public float spawnWait; public float startWait; public float waveWait; public GUIText scoreText; public GUIText waveCountText; private int waves; private int score; public GUIText restartText; public GUIText gameOverText;
private bool gameOver;
private bool restart;
void Start()
{
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
score = 0;
UpdateScore();
StartCoroutine(SpawnWaves());
}
private void Update()
{
if (restart)
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene("Main");
}
}
}
IEnumerator SpawnWaves()
{
waves = 0;
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
if (gameOver)
{
restartText.text = "Press 'R' for Restart";
restart = true;
break;
}
hazardCount++;
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "Score: " + score;
waveCountText.text = "Wave: " + waves;
}
public void GameOver()
{
gameOverText.text = "Game Over";
gameOver = true;
}
}