【问题标题】:Game Over Scene doesn't work (Unity C#)游戏结束场景不起作用(Unity C#)
【发布时间】:2016-03-04 07:31:34
【问题描述】:

我正在使用 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 秒,然后游戏会自动重新启动。代码有什么问题?

【问题讨论】:

  • 尝试在Application.LoadLevel(0)之后添加Console.Log("{{some text}}");并检查,也许它会在你现在期望的时候调用它。

标签: c# unity3d


【解决方案1】:

请删除; (位于 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);
}

【讨论】:

    【解决方案2】:

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

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

    然后在上面的代码中备份您想要启动 CoRoutine。 StartCoroutine("PauseFunc");

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多