猿问

无法在 Unity 中停止协程

我试图阻止玩家死亡时产生的波浪,但我似乎无法阻止协程。我真的不知道如何再攻击它,因为添加 if 语句和 break 不起作用。如何调用 StopCoroutine 并停止例程?我需要介绍新方法吗?


void Start () {

    gameOver = false;

    StartCoroutine (SpawnWaves());

}


void Update()

{

    if (gameOver)

    {

        StopCoroutine(SpawnWaves());

    }

  }


IEnumerator SpawnWaves()

{

    yield return new WaitForSeconds(startWait);

    while (true)

    {

        for (int i = 0; i < hazardCount; i++)

        {

            GameObject enemy = enemies[Random.Range(0, enemies.Length)];

            Instantiate(enemy, spawnPosition1, spawnRotation1);

            Instantiate(enemy, spawnPosition2, spawnRotation2);

            Instantiate(enemy, spawnPosition3, spawnRotation3);

            Instantiate(enemy, spawnPosition4, spawnRotation4);

            Instantiate(enemy, spawnPosition5, spawnRotation5);

            Instantiate(enemy, spawnPosition6, spawnRotation6);

            yield return new WaitForSeconds(spawnWait);

        }

        yield return new WaitForSeconds(waveWait);

        enemies[0].GetComponent<EnemyBehviour>().currentHealth *= enemyHealthMultiplier;

    }

}


HUH函数
浏览 175回答 4
4回答

繁华开满天机

当玩家死亡时,您需要将 gameOver 设置为 True您还需要添加逻辑以将 while 循环更改为 false 以退出 while 循环。例如在 while 循环内设置gameOver = truewhen并设置currentHealth == 0while(gameOver == false)

MM们

我认为问题可能是当您调用 StopCoroutine() 时,您并没有指示它停止协程的特定实例。相反,您需要存储对协程的引用并将其作为参数传递给您的 Stop。IEnumerator wavecoroutine = SpawnWaves();StartCoroutine(wavecoroutine);...StopCoroutine(wavecoroutine);

芜湖不芜

ryeMoss 实际上有正确的答案。您不需要更改 while 循环的条件,而是需要确保将参考值传递给该StopCoroutine方法。您可以在文档中看到这正是他们正在做的事情。问题是什么时候SpawnWaves调用它返回一个新IEnumerator的,当你试图阻止它时,这显然不是你想要的,哈哈。Start只需在您的方法内部更改为gameOver = false;waves = SpawnWaves(); <-- or whatever you want to call itStartCoroutine(waves); <-- using a reference然后传递waves给StopCoroutine.始终仔细阅读文档;在学习新的库、框架等时,他们是你最好的朋友。:)

慕的地10843

完全修复感谢@AlexG 的指导void Start () {&nbsp; &nbsp; gameOver = false;&nbsp; &nbsp; StartCoroutine (SpawnWaves());}IEnumerator SpawnWaves(){&nbsp; &nbsp; yield return new WaitForSeconds(startWait);&nbsp; &nbsp; while (gameOver != true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < hazardCount; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(gameOver)break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GameObject enemy = enemies[Random.Range(0, enemies.Length)];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Instantiate(enemy, spawnPosition1, spawnRotation1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Instantiate(enemy, spawnPosition2, spawnRotation2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Instantiate(enemy, spawnPosition3, spawnRotation3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Instantiate(enemy, spawnPosition4, spawnRotation4);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Instantiate(enemy, spawnPosition5, spawnRotation5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Instantiate(enemy, spawnPosition6, spawnRotation6);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return new WaitForSeconds(spawnWait);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; yield return new WaitForSeconds(waveWait);&nbsp; &nbsp; &nbsp; &nbsp; enemies[0].GetComponent<EnemyBehviour>().currentHealth *= eenter code herenemyHealthMultiplier;&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答