I am making a game in Unity C# and I want to make a Game Over scene.
I created a C# script ( GameOverScript
):
using UnityEngine;
using System.Collections;
public class GameOverScript : MonoBehaviour {
int score = 0;
void Start () {
score = PlayerPrefs.GetInt("Score");
}
void OnGUI()
{
GUI.Label(new Rect(Screen.width / 2 - 40, 50, 80, 30), "GAME OVER");
GUI.Label(new Rect(Screen.width / 2 - 40, 300, 80, 30), "Score: " + score);
if (GUI.Button(new Rect(Screen.width / 2 - 30, 350, 60, 30), "Retry?"));
{
Application.LoadLevel(0);
}
}
}
It works, but it just shows for about 2 seconds and the game restarts automatically. What's wrong with the code?
please delete ; (located after if condition)
GUI.Label(new Rect(Screen.width / 2 - 40, 300, 80, 30), "Score: " + score);
if (GUI.Button(new Rect(Screen.width / 2 - 30, 350, 60, 30), "Retry?")) //---->;
{
Application.LoadLevel(0);
}
Are you familiar with the IEnumerator function? The way I tend to delay is using WaitForSeconds()
. For Example,
IEnumerator PauseFunc(){
yield return new WaitForSeconds(5); //5 second pause
}
Then back up in your code above you'd want to start a CoRoutine. StartCoroutine("PauseFunc");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.