unity2Dで、一度しか実行していないコルーチンが複数呼ばれてしまいます。
whileの中の処理が終わった後、その後の処理は呼ばれず、コルーチンの初めにループしてしまいます。
以下、スクリプトです。
void Sample()
{
StartCoroutine(Test());
Debug.Log("ここは一度しか呼ばれていない");
}
public IEnumerator Test()
{
Debug.Log("ここが4回呼ばれている");
while (this.gameObject.transform.position.y > -2)
{
yield return new WaitForSeconds(0.01f);
transform.position -= transform.up * Time.deltaTime * 20;
}
yield return new WaitForSeconds(1f);
while (this.transform.position.x > 27)
{
yield return new WaitForSeconds(0.02f);
this.gameObject.transform.Translate(-0.05f, 0, 0);
}
yield return new WaitForSeconds(1f);
while (this.transform.position.x < 30) //ここを消すとループしない
{
yield return new WaitForSeconds(0.02f);
this.gameObject.transform.Translate(0.05f, 0.04f, 0); //yの値を0.02に変えるとループしない
}
Debug.Log("ここは一回しか呼ばれない");
回答1件