簡體   English   中英

Unity倒數計時器跳到零並倒數為負數

[英]Unity countdown timer jumping to zero and counting down into negatives

我的倒數計時器發生了一些奇怪的事情,我被困住了。 我正在向計時器傳遞一個數字(15秒),該計時器從15開始,然后跳到0,然后從那里倒數:0,-1,-2,-3 ...我在這里做錯了什么?

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

public class CountdownTimer : MonoBehaviour
{
    float seconds;
    public Text timerText;

    public void Update()
    {
        seconds -= Time.deltaTime;
        if (timerText != null)
        {
            timerText.text = Mathf.Round(seconds).ToString();
            Debug.Log (Mathf.Round(seconds));
        }
    }
}

我正在從另一個腳本中的函數調用倒計時腳本,如下所示:

void ShowRestartWarning()
    {
        //Debug.Log("Restart Warning Dialog");

        canvas = GameObject.FindGameObjectWithTag("Canvas");

        timerInstance = Instantiate(timeOutWarningDialog);
        timerInstance.transform.SetParent(canvas.transform, false);
        timerInstance.SetActive(true);

        CountdownTimer countdownTimer = timeOutWarningDialog.GetComponent<CountdownTimer>();
        countdownTimer.Update();                                                    

        CancelInvoke();
        Invoke("RestartGame", countdownLength);   
    }

嘗試使用協程而不是Update函數,這樣您就可以在需要時啟動和停止它。

public Text timerText;

public void Start(int seconds)
{
    StartCoroutine("RunTimer", seconds);
}

IEnumerator RunTimer(int seconds)
{
    while (seconds > 0)
    {
        if (timerText != null)
        {
            timerText.text = seconds.ToString();
            Debug.Log (seconds);
        }
        yield return new WaitForSeconds(1);
        seconds -= 1;
    }
}
public class CountdownTimer : MonoBehaviour
{
    float seconds;
    public Text timerText;

    public void Update()
    {
        seconds -= Time.deltaTime;
        if (timerText != null)
        {
            timerText.text = Mathf.Round(seconds).ToString();
            Debug.Log (Mathf.Round(seconds));
        }
    }
}

首先,被激活的MonoBehaviour中的Update()方法將在每一幀中調用(有關此內容的更多信息,請參見事件函數的執行順序

現在,您的腳本中有2個變量。 首先是私人seconds ,然后是公共timerText 為什么timerText是公共的? 如果該變量未被另一個類使用,則應為private

現在是時候您問了; 但是,如何在編輯器中附加Text組件? 好吧,您應該改為使用SerializeField

現在,我認為錯誤是您正在為文本組件分配一些值。 但是,您的可變seconds默認情況下為零。 (您聲明的浮點數沒有任何值,默認情況下它將initialized0f )。

因此,Text組件的第一幀將顯示您的初始值,假設15 但是,第一次調用Update() ,第二行seconds -= Time.deltaTime將類似於seconds = 0f - Time.deltaTime; ,然后將使用此新的almost zero值更新timerText

你能做什么? 您可以使用另一個事件函數“ 喚醒”Update調用之前為變量設置一些值。

如果要使用timerText的值,可以執行以下操作:

public class CountdownTimer : MonoBehaviour
{
    private float seconds;

    [SerializeField]
    private Text timerText;

    private void Awake()
    {
        if (!string.isNullOrEmpty(timerText))
        {
            seconds = float.Parse(timerText.text);          
        }
    }

    // Yep, Event Functions can be private and Unity will `call` them anyways
    private void Update() 
    {
        seconds -= Time.deltaTime;
        if (timerText != null)
        {
            timerText.text = Mathf.Round(seconds).ToString();
            Debug.Log (Mathf.Round(seconds));
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM