游戏结束场景不起作用(Unity C#)

本文关键字:Unity 不起作用 游戏结束 | 更新日期: 2023-09-27 18:37:15

我正在用Unity C#制作一个游戏,我想制作一个Game Over场景。

我创建了一个 C# 脚本 ( 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);
    }
}
}

它可以工作,但它只显示大约 2 秒,游戏会自动重新启动。代码有什么问题?

游戏结束场景不起作用(Unity C#)

请删除;(位于 if 条件之后)

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);
}

你熟悉IEnumerator函数吗?我倾向于延迟的方式是使用 WaitForSeconds() .例如

IEnumerator PauseFunc(){
yield return new WaitForSeconds(5); //5 second pause
}

然后备份上面的代码,你想要启动一个协程。StartCoroutine("PauseFunc");