2017-07-17 4 views
-2

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

"あなたのコードではcountdownLength(これは5に設定されています)"は正しくありません。あなたはそれを初期化しません。したがって、それは0に設定されています。 – jross

+2

あなたが投稿したコードにリテラル '5' _anywhere_は表示されません。助けが必要な場合は、実際に起こっていると主張する行動を実際に再現する良い[mcve]を投稿してください。上記のコードは最小ではなく、実際にあなたが主張していることをしません。 –

+0

ランダムではありません。ゲームのタイマーが始まり、毎秒逆方向にカウントされます。 –

答えて

1

あなたの変数を初期化します。 float s = countdownLength--;宣言S = 0.0fをオン

から5 ===> -5最初の値

あなたのゲームを再起動するために-1値に達することはありません。 到達する方法はこれを変更しています:

if (s <= -1) 
{ 
    RestartGame(); 
} 
関連する問題