Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d unity StartContine在我从另一个脚本调用它时不起作用_Unity3d_Ienumerator - Fatal编程技术网

Unity3d unity StartContine在我从另一个脚本调用它时不起作用

Unity3d unity StartContine在我从另一个脚本调用它时不起作用,unity3d,ienumerator,Unity3d,Ienumerator,我有一个带有while循环的IEnumerator,当我在同一个脚本中从start方法调用它时,它应该运行100次,这非常有效。但是当我从另一个脚本调用IEnumerator时,它会被调用,但是while循环只运行一次 public class script1: MonoBehaviour { private void Start() { //StartCoroutine(StartCountdown(3)); // When I call i

我有一个带有while循环的IEnumerator,当我在同一个脚本中从start方法调用它时,它应该运行100次,这非常有效。但是当我从另一个脚本调用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);
这受时间刻度的影响

使用缩放时间将协同路由执行暂停给定的秒数

暂停的实时时间等于给定时间除以time.timeScale

对于时间刻度=0的情况,根本不需要进一步调用它

你应该改用


您是否可能禁用或销毁script2?请注意,尽管协同程序是在script1中定义的,但它将在script2上运行!否则,您需要执行circle.GetComponent。StartCustomer circle.GetComponent.StartCountdown3;为了在script1或更好的情况下运行该例程,请查找错误。在GameOver方法中,我将Time.timescale设置为0;我不知道为什么这会影响IEnumerator,但现在我知道了问题所在。谢谢你的回答
yield return new WaitForSecondsRealtime(waiteTime);