2017-07-17 29 views
0

我收到一條錯誤消息,我不確定如何解決。我試圖在經過短暫的空閒時間後開始倒計時,然後開始第二次與視覺警告配對的倒計時。只要協程啓動,我得到這個錯誤:由於不活動的遊戲對象,協程不會啓動

協程無法啓動,因爲遊戲對象'_CountdownTimer'不活動! UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator的) CountdownTimer:StartPreCountTimer()(在資產/ _Components/_Scripts/CountdownTimer.cs:38) 遊戲管理:CheckUserActivity()(在資產/ _Components/_Scripts/GameManager.cs:68)

我缺少什麼?我需要在哪裏設置_CountdownTimer的活動狀態?謝謝!!

GameManager.cs

using UnityEngine; 
using UnityEngine.SceneManagement; 

public class GameManager : MonoBehaviour 
{ 
    public static GameManager gameManagerInstance = null; // Create Singleton 

    public float checkUserActivityInterval; 
    public GameObject loader; 
    public GameObject countdownTimer; 
    private GameObject gameManager; 

    private Vector3 currentMousePosition; 
    private Vector3 prevMousePosition; 
    private CountdownTimer countdownInstance; 
    private Scene currentScene; 
    public Color defaultBackgroundColor;     
    public Object startingScene; 

    public static bool userActive; 
    public static bool preCountActive; 
    public static bool restartWarningActive; 

    public static string animalDataFilePathJSON; 
    public static string animalDataFilePathTex; 

    void Awake() 
    { 
     if (CountdownTimer.countdownTimerInstance == null) 
      Instantiate(countdownTimer); 

     if (gameManagerInstance == null) 
      gameManagerInstance = this; 
     else if (gameManagerInstance != null) 
      Destroy(gameObject); 

     DontDestroyOnLoad(gameObject); 
    } 

    void Start() 
    { 
     prevMousePosition = Input.mousePosition; 
     countdownInstance = countdownTimer.GetComponent<CountdownTimer>(); // Create an instance of CountdownTimer 
     InvokeRepeating("CheckUserActivity", 0, checkUserActivityInterval); 
     InvokeRepeating("SetPrevMousePosition", 0, checkUserActivityInterval); 
    } 

    void Update() 
    { 
     currentScene = SceneManager.GetActiveScene(); 
     currentMousePosition = Input.mousePosition; 
    } 

    void CheckUserActivity() 
    { 
     if (currentScene.name != startingScene.name) 
     { 
      if (currentMousePosition == prevMousePosition) 
      { 
       Debug.Log("MOUSE HAS NOT MOVED!!"); 
       userActive = false; 

       if (!userActive && !preCountActive) 
        countdownInstance.StartPreCountTimer(); 
      } 

      if (currentMousePosition != prevMousePosition) 
      { 
       Debug.Log("MOUSE HAS MOVED!!"); 
       userActive = true; 

       if (preCountActive == true) 
        countdownInstance.RestartPreCountTimer(); 
      } 
     } 
    } 

    void SetPrevMousePosition() 
    { 
     prevMousePosition = Input.mousePosition; 
    } 
} 

CountdownTimer.cs

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; 

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

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

     counter = RunTimer(countdownDelay); // create new reference to counter 
     StartCoroutine(counter); 
    } 

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

    void ShowRestartWarning() 
    { 
     GameManager.preCountActive = false; 
     GameManager.restartWarningActive = true; 

     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(StopTimer); // add button listener 

     if (timerDialogBoxInstance.activeInHierarchy == true) 
     { 
      counter = RunTimer(countdownLength); // create new reference to counter, resets countdown to countdownLength 
      StartCoroutine(counter); 
     } 
    } 

    IEnumerator RunTimer(float seconds) 
    { 
     float s = seconds; 
     while (s > -1) 
     { 
      if (GameManager.restartWarningActive == true) 
       if (timerTextField != null) 
        timerTextField.text = s.ToString(); 

      yield return new WaitForSeconds(countdownInterval); 
      s -= countdownInterval; 
     } 

     if (s == -1) 
     { 
      if (GameManager.restartWarningActive == true) 
       RestartGame(); 
     } 
    } 

    void StopTimer() 
    { 
     Debug.Log("Restart Cancelled"); 
     StopCoroutine(counter); 
     Destroy(timerDialogBoxInstance); 
    } 

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

您將倒數腳本附加到的遊戲對象在哪裏?檢查它是否有效。 –

+0

@SurajS CountdownTimer.cs附加到_CountdownTimer預製件,該預製件在Awake的GameManager.cs中被實例化。 – greyBow

+0

_CountdownTimer的父節點是否在任何時候處於非活動狀態?如果是這樣,你必須使用'timerInstance.activeInHierarchy'而不是'timerInstance.activeSelf'來使其工作 –

回答

1

我想我知道你的錯誤。當您創建countdownTimer的實例時。您必須存儲對其的引用,才能獲取底層的CountdownTimer類。

嘗試這樣做,在你的遊戲管理

private GameObject countDownTimerInstance; 

甲戌()

countDownTimerInstance = Instantiate(countdownTimer); 

,並在開始()做,

countdownInstance = countdownTimerInstance.GetComponent<CountdownTimer>(); 

我認爲問題在於你直接訪問預製件的getComponent()而不是實例化的gameObject的 CountdownTimer !.

+0

我應該在GameManager類還是CountdownTimer類中進行這些更改? – greyBow

+0

GameManager Class @greyBow –

+0

謝謝你的幫助! – greyBow