How to add "Press ESC to EXIT"? and exit the game?
Hello, To my spaceshooter tutorial game I want to add an Exit/Closing the game function. How do I do that?
Comment
Answer by liorb · Jul 24, 2017 at 10:38 AM
I'm having trouble.
This is the "Done_GameController" script in which i've added if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); }
Pressing ESC do nothing, I don't undersatand why?
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);
}
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
}
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;
}
}