我一直在关注如何在YouTube上制作计时器的教程,但我现在因为Unity 5中的GUIText
问题而陷入困境。这是我的代码:
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:键入&#39; UnityEngine.GameObject&#39;不包含 &#39; GUIText&#39;的定义没有扩展方法&#39; GUIText&#39;类型 &#39; UnityEngine.GameObject&#39;可以找到(你错过了使用 指令或程序集引用?)
如果没有GUIText
答案 0 :(得分:2)
只需选择您为其分配此代码的Gameobject,然后在Inspector
视图中点击Add Component
并向其中添加GUI Text
组件。
不要忘记为它设置字体
答案 1 :(得分:2)
您可以使用GameObject.Find("TimerText").GUIText.text
方法重写它,而不是GetComponent(string)
,这是正确的方法:
GameObject.Find("TimerText").GetComponent<GUIText>().text =
Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");