2017-06-12 39 views
0

我收到「協程無法啓動,因爲遊戲對象'TimeOutWarningDialog'處於非活動狀態!」錯誤但我不確定爲什麼會出現此錯誤。協程無法啓動,因爲遊戲對象無效

只給代碼的破敗:

  1. 我正在尋找在閒置GameManger.Update()

  2. 如果閒置一段時間我打電話GameManager.ShowRestartWarning( )

  3. TimeOutWarningDialog得到SETACTIVE真

  4. 我檢查,如果該對象是激活前調用StartRestartTimer(),if(timerInstance.activeSelf == true)StartRestartTimer();

  5. 我在CountdownTimer類調用startTimer所()

我設置我調用startTimer所功能其中包括協程之前我instatiating到「活動」的對象。我在這裏做錯了什麼? 任何幫助將是偉大的!

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

public class CountdownTimer : MonoBehaviour 
{ 
    public float countdownLength; 
    public Text timerText; 
    public bool stop = true; 
    private float minutes; 
    private float seconds; 

    public void startTimer(float from) 
    { 
     stop = false; 
     countdownLength = from; 
     Update(); 
     StartCoroutine(updateCoroutine()); 
    } 

    void Update() 
    { 
     if (stop) return; 
     countdownLength -= Time.deltaTime; 

     minutes = Mathf.Floor(countdownLength/60); 
     seconds = countdownLength % 60; 
     if (seconds > 59) seconds = 59; 
     if (minutes < 0) 
     { 
      stop = true; 
      minutes = 0; 
      seconds = 0; 
     } 
    } 

    private IEnumerator updateCoroutine() 
    { 
     while (!stop) 
     { 
      timerText.text = string.Format("{0:0}:{1:00}", minutes, seconds); 
      yield return new WaitForSeconds(0.2f); 
      Debug.Log(string.Format("{0:0}:{1:00}", minutes, seconds)); 
     } 
    } 
} 
+0

也許其中一個祖先,比如'canvas.transform',是不活躍的? – Dunno

回答

1

的問題是這種方法:

void StartRestartTimer() 
{ 
    CountdownTimer countdownTimer = timeOutWarningDialog.GetComponent<CountdownTimer>(); 
    countdownTimer.startTimer(countdownLength); 

    CancelInvoke(); 
    Invoke("RestartGame", countdownLength); 
} 

你第一次啓動協同程序,然後調用RestartGame

using UnityEngine; 
using System.Collections.Generic; 
using UnityEngine.SceneManagement; 

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

    // Set Default Background Color 
    public Color defaultColor; 

    // Restart variables 
    private Vector3 prevMousePosition; 
    public GameObject timeOutWarningDialog; 
    public GameObject restartDialog; 
    public float countdownLength; 
    public float timeUntilCountdown; 

    // Game Controller 
    private GameObject canvas; 
    private GameObject gameManager; 
    public GameObject timerInstance; 
    public Object startingScene; 
    private Scene currentScene; 

    // File System List of Folders 
    public List<string> folders; 

    void Awake() 
    { 
     if (instance == null) 
      instance = this; 
     else if (instance != null) 
      Destroy(gameObject); 

     DontDestroyOnLoad(gameObject); 

     gameManager = GameObject.FindGameObjectWithTag("GameManager"); 
    } 

    void Start() 
    {  
     prevMousePosition = Input.mousePosition; 
     currentScene = SceneManager.GetActiveScene(); 
    } 

    void Update() 
    { 
     if(Input.anyKeyDown || Input.mousePosition != prevMousePosition) 
      if(currentScene.name != startingScene.name) 
       StartGameTimer(); 
     prevMousePosition = Input.mousePosition; 
    } 

    // GAME TIMER 

    void StartGameTimer() 
    { 
     // Debug.Log("Game Timer Started"); 
     CancelInvoke(); 

     if (GameObject.FindGameObjectWithTag("Timer") == null) 
      Invoke("ShowRestartWarning", timeUntilCountdown); 
    } 

    void ShowRestartWarning() 
    { 
     canvas = GameObject.FindGameObjectWithTag("Canvas"); 

     timerInstance = Instantiate(timeOutWarningDialog); 
     timerInstance.transform.SetParent(canvas.transform, false); 
     timerInstance.SetActive(true); 

     if (timerInstance.activeSelf == true) 
      StartRestartTimer(); 
    } 

    void StartRestartTimer() 
    { 
     CountdownTimer countdownTimer = timeOutWarningDialog.GetComponent<CountdownTimer>(); 
     countdownTimer.startTimer(countdownLength); 

     CancelInvoke(); 
     Invoke("RestartGame", countdownLength); 
    } 

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

     Debug.Log("Game Restarted"); 
     Debug.Log("Current Scene is " + currentScene.name + "."); 
    } 

    void DestroyTimer() 
    { 
     Destroy(GameObject.FindGameObjectWithTag("Timer")); 
    } 
} 

然後我在下面的CountdownTimer類調用startTimer所加載另一個場景。所以與協程的對象被破壞。


,因爲它需要對您的場景更多的知識,但你可能要嘗試添加劑場景加載我不能給你解決。