首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一5中的GUIText (需要一个替代方案)

统一5中的GUIText (需要一个替代方案)
EN

Stack Overflow用户
提问于 2015-06-08 16:21:35
回答 2查看 3.2K关注 0票数 1

我一直在学习如何在YouTube上制作定时器的教程,但是由于Unity5中的GUIText问题,我现在陷入了困境。

代码语言:javascript
复制
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’(您是缺少使用指令还是程序集引用?)

在没有GUIText的情况下,我能做什么使计时器在我的游戏中显示?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-08 16:30:43

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

代码语言:javascript
复制
GameObject.Find("TimerText").GetComponent<GUIText>().text = 
    Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");
票数 2
EN

Stack Overflow用户

发布于 2015-06-08 16:30:40

只需选择指定此代码的游戏对象,然后在Inspector视图中单击Add Component并向其添加GUI Text组件。

别忘了为它设置字体

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30714357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档