2017-07-17 2 views
-2

Mon compte à rebours dans ShowRestartDialog() agit funky. Au lieu de commencer au compte countdownLength défini (qui est mis à 5), il commence à un nombre négatif aléatoire et descend à partir de là. Pourquoi cela se produirait-il? Merci!Compte à rebours commençant à un nombre négatif aléatoire

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 (qui est réglé sur 5) » - n'est pas vrai dans votre code. Vous ne l'initialisez pas. Par conséquent, il est défini sur 0. – jross

+2

Je ne vois même pas le '5' _anywhere_ littéral dans le code que vous avez posté. Si vous voulez de l'aide, publiez un bon [mcve] qui reproduise le comportement que vous prétendez. Le code ci-dessus n'est pas minimal et ne fait pas ce que vous prétendez faire. –

+0

Ce n'est pas aléatoire. Avec la minuterie de jeu commence aussi et compte à rebours dans toutes les secondes. –

Répondre

1

Vous initialisez votre variable s incorrecte. float s = countdownLength--;

Sur déclaration s = 0.0f - 5 ===> -5 première valeur

Vous ne parviennent jamais à la valeur -1 pour redémarrer votre jeu. Une façon d'atteindre est en train de changer ceci:

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