当我从另一个脚本调用它时,unity StartCoroutine 不工作
unity StartCorountine not woking when I call it from another script
我有一个带有 while 循环的 IEnumerator,它应该 运行 100 次,当我从同一个脚本中的 start 方法调用它时,它工作得很好。但是当我从另一个脚本调用 IEnumerator 时,它被调用但是 while 循环只 运行 一次。
public class script1: MonoBehaviour
{
private void Start()
{
//StartCoroutine(StartCountdown(3));
// When I call it here the while loop runs 100
}
public IEnumerator StartCountdown(float countdownValue)
{
float waiteTime = countdownValue / 100;
int loopNumber = 100;
//float idk = 0;
while (loopNumber > 0)
{
Debug.Log("aufgerufen");
circle.fillAmount -= 0.01f;
yield return new WaitForSeconds(waiteTime);
loopNumber -= 1;
}
}
}
public class script2 : MonoBehaviour
{
public void GameOver()
{
panel.SetActive(true);
Time.timeScale = 0;
GameObject circle = GameObject.FindGameObjectWithTag("LoadCircle");
StartCoroutine(circle.GetComponent<LoadCircle>().StartCountdown(3));
//When I call it from here the while loop only run once
}
}
您正在使用
Time.timeScale = 0;
然后
yield return new WaitForSeconds(waiteTime);
受时间尺度影响!
Suspends the coroutine execution for the given amount of seconds using scaled time.
The real time suspended is equal to the given time divided by Time.timeScale
对于时间刻度 = 0,根本不会进一步调用它。
您应该改用 WaitForSecondsRealtime
yield return new WaitForSecondsRealtime(waiteTime);
我有一个带有 while 循环的 IEnumerator,它应该 运行 100 次,当我从同一个脚本中的 start 方法调用它时,它工作得很好。但是当我从另一个脚本调用 IEnumerator 时,它被调用但是 while 循环只 运行 一次。
public class script1: MonoBehaviour
{
private void Start()
{
//StartCoroutine(StartCountdown(3));
// When I call it here the while loop runs 100
}
public IEnumerator StartCountdown(float countdownValue)
{
float waiteTime = countdownValue / 100;
int loopNumber = 100;
//float idk = 0;
while (loopNumber > 0)
{
Debug.Log("aufgerufen");
circle.fillAmount -= 0.01f;
yield return new WaitForSeconds(waiteTime);
loopNumber -= 1;
}
}
}
public class script2 : MonoBehaviour
{
public void GameOver()
{
panel.SetActive(true);
Time.timeScale = 0;
GameObject circle = GameObject.FindGameObjectWithTag("LoadCircle");
StartCoroutine(circle.GetComponent<LoadCircle>().StartCountdown(3));
//When I call it from here the while loop only run once
}
}
您正在使用
Time.timeScale = 0;
然后
yield return new WaitForSeconds(waiteTime);
受时间尺度影响!
Suspends the coroutine execution for the given amount of seconds using scaled time.
The real time suspended is equal to the given time divided by
Time.timeScale
对于时间刻度 = 0,根本不会进一步调用它。
您应该改用 WaitForSecondsRealtime
yield return new WaitForSecondsRealtime(waiteTime);