无法将类型'string'隐式转换为UnityEngine.UI.text

时间:2019-05-25 07:37:55

标签: c# unity3d

尝试在Unity中的3D对象Textmesh上实现倒数计时器脚本,并由于无法将字符串转换为UI.text而被困在此区域

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

public class TimerScript : MonoBehaviour
{
public Text TimerText;

float CurrentTime = 0f;
float StartTime = 10f;

void Start()
{
    CurrentTime = StartTime;
}


void Update()
{
    float t = CurrentTime -= 1 * Time.deltaTime;
    //TimerText.text = CurrentTime.ToString("0");

    string mins = ((int) t / 60).ToString();
    string secs = (t % 60).ToString("f1");
    TimerText = mins + ":" + secs;


    if (CurrentTime <= 0)
    {
        CurrentTime = 0;
    }
}
}

1 个答案:

答案 0 :(得分:0)

您的TimerText应该引用text属性来设置其文本。

代码:

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

public class TimerScript : MonoBehaviour
{
public Text TimerText;

float CurrentTime = 0f;
float StartTime = 10f;

void Start()
{
CurrentTime = StartTime;
}


void Update()
{
float t = CurrentTime -= 1 * Time.deltaTime;
//TimerText.text = CurrentTime.ToString("0");

string mins = ((int) t / 60).ToString();
string secs = (t % 60).ToString("f1");
TimerText.text = mins + ":" + secs;


if (CurrentTime <= 0)
{
    CurrentTime = 0;
}
}
}