我遇到的问题是我的Unity GUI.Buttons在这个脚本中被点击时没有运行代码(它们在另一个脚本中以类似的方式工作),我想知道为什么。我可以说没有发生任何事情,因为即使是声音也不会播放,更不用说现场过渡了。
为了给出一些背景知识,我希望有一个计时器,它会停止并显示玩家完成关卡所花费的时间,尽管这个位看起来不错。然后他们可以通过3个按钮决定下一步该做什么:下一级,菜单或退出。
以下是整个Unity C#脚本:
using UnityEngine;
using UnityEngine.SceneManagement;
public class finishScript : MonoBehaviour {
public GUISkin skin;
public bool finished = false;
private float timer = 0.0f;
AudioSource a;
public AudioClip clickSound;
void Start ()
{
a = GetComponent<AudioSource>();
}
void Update ()
{
if (!finished) { timer += Time.deltaTime; }
}
void OnGUI()
{
GUI.skin = skin;
if (!finished)
{
GUI.Box(new Rect(2, 2, Screen.width / 8, Screen.height / 16), "Time: " + Mathf.Round(timer).ToString());
}
if (finished)
{
GUI.Box(new Rect(Screen.width/4, Screen.height/4, Screen.width/2, Screen.height/4),
"Congratulations!\n\nYou finished the level in:\n" + Mathf.Round(timer).ToString());
if (GUI.Button(new Rect(Screen.width/4, Screen.height/2, Screen.width/8, Screen.height/8), "Main Manu"))
{
a.PlayOneShot(clickSound, 3f);
SceneManager.LoadScene("main_menu");
}
if (GUI.Button(new Rect(3*Screen.width / 8, Screen.height / 2, Screen.width / 8, Screen.height / 8), "Next Level"))
{
a.PlayOneShot(clickSound, 3f);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
if (GUI.Button(new Rect( 3 * Screen.width / 4, Screen.height / 2, Screen.width / 8, Screen.height / 8), "Quit"))
{
a.PlayOneShot(clickSound, 3f);
Application.Quit();
}
}
}
}
所以我只是想知道这有什么特别的错误,为什么,谢谢!