how could i change this code so the restart function can be activated via button?
using UnityEngine; using System.Collections;
public class Done_GameController : MonoBehaviour { public GameObject[] hazards; public Vector3 spawnValues; public int hazardCount; public float spawnWait; public float startWait; public float waveWait;
public GUIText scoreText;
public GUIText restartText;
public GUIText gameOverText;
private bool gameOver;
private bool restart;
private int score;
void Start ()
{
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
void Update ()
{
if (restart)
{
if (Input.GetKeyDown (KeyCode.R))
{
Application.LoadLevel (Application.loadedLevel);
}
}
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards [Random.Range (0, hazards.Length)];
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;
}
}
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
public void GameOver ()
{
gameOverText.text = "Game Over!";
gameOver = true;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612055532im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Giving a UI button a Color at Start 0 Answers
EventTrigger on button not recognizing instantsiated player 1 Answer
EditorWindow: Toolbar - Button cross position 0 Answers
OnLevelWasLoaded deprecated, trying to setup my buttons 1 Answer
Is there any way you can make a button that is placed in 2D space and not the canvas? 1 Answer