2017-07-20 6 views
0

なぜこのようなことが起こっているのか分かりませんが、StopTimerButton()を呼び出すボタンを押したときに停止するカウントダウンタイマーがあります。しかし、StopCoroutineを呼び出しても、タイマーはゼロに達するまでカウントダウンし続け、RestartGame()を呼び出します。私のコードでは何が分かりませんか?StopCoroutineの後にタイマーが停止しない

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; 
    public float preCountdownLength; 
    public float countdownLength; 

    private GameObject timerDialogBoxInstance; 
    private GameObject timerDialogCountdownText; 
    private Text timerDialogCountdownTextTarget; 
    private GameObject canvas; 
    private IEnumerator warningCounter; 
    private IEnumerator preCounter; 
    private Button stopCountButton; 
    private float countdownInterval = 1.0f; 
    private bool preCountActive; 
    private bool warningCountActive; 

    void Awake() 
    { 
     ResetCountStates(); 

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

    void Update() 
    { 
     bool userActive = GameManager.userActive; 
     bool onIntroScreen = GameManager.onIntroScreen; 

     if (!userActive && !onIntroScreen && !preCountActive) 
      StartPreCountTimer(preCountdownLength); 
     else if (userActive && !onIntroScreen && preCountActive) 
      StopPreCountTimer(); 
    } 

    void StartPreCountTimer(float length) 
    { 
     preCountActive = true; 
     preCounter = RunTimer(length); 
     StartCoroutine(preCounter); 
     Debug.Log("PreCount Started"); 
    } 

    void StopPreCountTimer() 
    { 
     preCountActive = false; 
     StopCoroutine(preCounter); 
     Debug.Log("PreCount Stopped"); 
    } 

    void WarningDialog(float length) 
    { 
     preCountActive = false; 
     warningCountActive = true; 
     canvas = GameObject.FindGameObjectWithTag("Canvas"); 
     timerDialogBoxInstance = Instantiate(timeOutWarningDialog); // instantiate timeout warning dialog 
     timerDialogBoxInstance.transform.SetParent(canvas.transform, false); 
     timerDialogBoxInstance.SetActive(true); 
     timerDialogCountdownText = GameObject.FindGameObjectWithTag("CountdownText"); // get reference to countdown text GO 
     timerDialogCountdownTextTarget = timerDialogCountdownText.GetComponent<Text>(); // get countdown textfield component 
     stopCountButton = timerDialogBoxInstance.GetComponentInChildren<Button>(); // get reference to keep playing button 
     stopCountButton.onClick.AddListener(StopTimerButton); // add button listener 

     if (warningCountActive && !preCountActive && timerDialogBoxInstance != null) 
     { 
      warningCounter = RunTimer(length); // create new reference to counter, resets countdown to countdownLength 
      StartCoroutine(warningCounter); 
     } 
    } 

    IEnumerator RunTimer(float seconds) 
    { 
     // PRECOUNT TIMER 
     if (!warningCountActive) 
     { 
      float s = seconds; 
      while (s > 0) 
      { 
       yield return new WaitForSeconds(countdownInterval); 
       s -= countdownInterval; 
       Debug.Log("PreCount: " + s); 
      } 

      if (s == 0) 
      { 
       preCountActive = false; 
       warningCountActive = true; 
       WarningDialog(countdownLength); 
      } 
     } 

     // WARNING DIALOG TIMER 
     if (warningCountActive) 
     { 
      float s = seconds; 
      while (s > 0) 
      { 
       yield return new WaitForSeconds(countdownInterval); 
       if (timerDialogBoxInstance != null) 
        timerDialogCountdownTextTarget.text = s.ToString(); 
       s -= countdownInterval; 
       Debug.Log("WarningCountdown: " + s); 
      } 

      if (s == 0) 
       if (timerDialogBoxInstance != null) 
       { 
        StopCoroutine(warningCounter); 
        Destroy(timerDialogBoxInstance); 
        RestartGame(); 
       } 
     } 
    } 

    void StopTimerButton() 
    { 
     warningCountActive = false; 
     StopCoroutine(warningCounter); 

     if (timerDialogBoxInstance != null) 
     {  
      Destroy(timerDialogBoxInstance); 
      timerDialogBoxInstance = null; 
     } 
     Debug.Log("Restart Cancelled"); 
    } 

    void ResetCountStates() 
    { 
     preCountActive = false; 
     warningCountActive = false; 
    } 

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

「WarningDialog」関数を呼び出すと、 – Programmer

+0

PreCountTimerが0に達した後(Runtimerで)WarningDialog()を呼び出します。 – greyBow

+0

そして 'StopTimerButton'関数で停止したいタイマーをどうやって起動しますか? – Programmer

答えて

0

スクリプトが問題を解明するには時間がかかりすぎますが、問題になる可能性があることに気付きました。あなたのWarningDialog機能で

、このコードブロックを見て:たびにRunTimer機能がまだ実行されているので、

if (warningCountActive && !preCountActive && timerDialogBoxInstance != null) 
{ 
    warningCounter = RunTimer(length); // create new reference to counter, resets countdown to countdownLength 
    StartCoroutine(warningCounter); 
} 

StopTimerButton()機能が動作していない、あなたは、コルーチン関数リファレンスWarningDialog関数を呼び出しますを実行すると上書きされるため、warningCounterという名前のファイルは上書きされます。

あなたはwarningCounterのみ一度初期化されるようにStart()機能へwarningCounter = RunTimer(length);を移動する必要があります。

+0

これは試しましたか? – Programmer

関連する問題