倒计时从随机负数开始

时间:2017-07-17 04:36:21

标签: c# unity3d invoke

我在ShowRestartDialog()中的倒数计时器表现得很时髦。它不是从定义的countdownLength(设置为5)开始,而是从一个随机的负数开始,然后从那里开始。为什么会这样?谢谢!

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

public class CountdownTimer : MonoBehaviour
{
    public static CountdownTimer countdownTimerInstance = null; // Create Singleton

    public Object startingScene;
    public GameObject timeOutWarningDialog;
    private GameObject timerDialogBoxInstance;
    private GameObject canvas; 

    private IEnumerator counter;
    private Button stopCountButton;
    private Text timerTextField;

    public float countdownLength;
    public float countdownDelay;
    private float countdownInterval = 1.0f;

    void Awake()
    {
        if (countdownTimerInstance == null)
            countdownTimerInstance = this;
        else if (countdownTimerInstance != null)
            Destroy(gameObject);
        DontDestroyOnLoad(gameObject);
    }

    public void StartPreCountTimer()
    {
        GameManager.preCountActive = true;

        Debug.Log("StartPreCountTimer Timer has Started!");

        if (GameManager.restartWarningActive == false)
            Invoke("ShowRestartDialog", countdownDelay);
    }

    public void RestartPreCountTimer()
    {
        GameManager.preCountActive = false;

        Debug.Log("StartPreCountTimer Timer has Restarted!");
            CancelInvoke("ShowRestartDialog");
    }

    void ShowRestartDialog()
    {
        GameManager.preCountActive = false;

        canvas = GameObject.FindGameObjectWithTag("Canvas");

        timerDialogBoxInstance = Instantiate(timeOutWarningDialog); // instantiate timeout warning dialog
        timerDialogBoxInstance.transform.SetParent(canvas.transform, false);
        timerDialogBoxInstance.SetActive(true);

        Text[] textFields = timerDialogBoxInstance.GetComponentsInChildren<Text>(true); // get reference to timer textfields
        timerTextField = textFields[2]; // access and assign countdown textfield

        stopCountButton = timerDialogBoxInstance.GetComponentInChildren<Button>(); // get reference to keep playing button
        stopCountButton.onClick.AddListener(StopDialogTimer); // add button listener

        if (timerDialogBoxInstance.activeInHierarchy == true)
            InvokeRepeating("StartDialogTimer", 0, countdownInterval);
    }

    void StartDialogTimer()
    {
        float s = countdownLength--;

        Debug.Log(s);

        if (timerTextField != null)
            timerTextField.text = s.ToString();

        if (s == -1)
        {
            RestartGame();
        }
    }

    void StopDialogTimer()
    {
        Debug.Log("Restart Cancelled");
        CancelInvoke("StartDialogTimer");
        Destroy(timerDialogBoxInstance);
    }

    void RestartGame()
    {
        SceneManager.LoadScene(startingScene.name);
    }
}

1 个答案:

答案 0 :(得分:1)

你初始化你的变量。     float s = countdownLength - ;

声明s = 0.0f - 5 ===&gt; -5第一个值

你永远不会达到-1值来重启你的游戏。 达成的方法正在改变这一点:

if (s <= -1)
{
    RestartGame();
}