倒计时器NullReferenceException

时间:2017-12-27 08:23:47

标签: c# unity5

我正在尝试在Unity中创建一个计时器,但总是遇到错误“NullReferenceException:对象引用未设置为对象的实例”

这是我的代码

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

public class Timer : MonoBehaviour{

public int timeLeft = 5;
public Text countdownText;

// Use this for initialization
void Start()
{
    StartCoroutine("LoseTime");
}

// Update is called once per frame
void Update()
{
    countdownText.text = ("Time Left = " + timeLeft);

    if (timeLeft <= 0)
    {
        StopCoroutine("LoseTime");
        countdownText.text = "Times Up!";
    }
}

IEnumerator LoseTime()
{
    while (true)
    {
        yield return new WaitForSeconds(1);
        timeLeft--;
    }
}
}

请帮助

0 个答案:

没有答案