1

我一直在关注如何在 YouTube 上制作计时器的教程,但由于GUITextUnity 5 中的问题,我现在陷入困境。这是我的代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Timer : MonoBehaviour {

    public float Seconds = 59;
    public float Minutes = 0;

    void Update() {

        if (Seconds <= 0) {

            Seconds = 59;
            if (Minutes > 1) {

                Minutes--;

            } else {

                Minutes = 0;
                Seconds = 0;
                //This makes the guiText show the time as X:XX. ToString.("f0") formats it so there is no decimal place.
                GameObject.Find("TimerText").GUIText.text = Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");

            }

        } else {

            Seconds -= Time.deltaTime;

        }

        //These lines will make sure that the time is shown as X:XX and not X:XX.XXXXXX
        if(Mathf.Round(Seconds) <= 9) {

            GameObject.Find("TimerText").GUIText.text = Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");

        } else {

            GameObject.Find("TimerText").GUIText.text = Minutes.ToString("f0") + ":" + Seconds.ToString("f0");

        }

    }

}

问题出在GUIText.

错误 CS1061:“UnityEngine.GameObject”类型不包含“GUIText”的定义,并且找不到“UnityEngine.GameObject”类型的扩展方法“GUIText”(您是否缺少 using 指令或程序集引用?)

如果没有 ,我该怎么做才能在我的游戏中显示计时器GUIText

4

2 回答 2

2

GameObject.Find("TimerText").GUIText.text您可以使用方法重写它,而不是这样做,GetComponent(string)这是正确的方法:

GameObject.Find("TimerText").GetComponent<GUIText>().text = 
    Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");
于 2015-06-08T16:30:43.130 回答
1

只需选择您为其分配此代码的游戏对象,然后在Inspector视图中单击Add Component并向其添加GUI Text组件。
不要忘记为其设置字体

于 2015-06-08T16:30:40.217 回答