我收到「協程無法啓動,因爲遊戲對象'TimeOutWarningDialog'處於非活動狀態!」錯誤但我不確定爲什麼會出現此錯誤。協程無法啓動,因爲遊戲對象無效
只給代碼的破敗:
我正在尋找在閒置GameManger.Update()
如果閒置一段時間我打電話GameManager.ShowRestartWarning( )
TimeOutWarningDialog得到SETACTIVE真
我檢查,如果該對象是激活前調用StartRestartTimer(),if(timerInstance.activeSelf == true)StartRestartTimer();
我在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));
}
}
}
也許其中一個祖先,比如'canvas.transform',是不活躍的? – Dunno