Can't stop a Coroutine in Unity

Ryan W

I'm trying to stop waves spawning upon player death but I cant seem to stop the coroutine. I don't really know how to attack this any more because adding if statements and breaks didn't work. How do I call StopCoroutine and have the routine stop? Would I need to introduce new methods and such?

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;
    }
}
AlexG
  1. You need to set gameOver to True when the player dies
  2. You also need to add logic to change while loop to false to get out of the while loop.

For example set gameOver = true when currentHealth == 0 inside the while loop and set while(gameOver == false)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Can't get componentDidUpdate() stop looping

分類Dev

Can't stop the timer. JavaScript

分類Dev

jquery scrollTop slideDown can't stop repeat

分類Dev

apache2 can't stop and nginx can't start

分類Dev

Why can't i log in on Unity?

分類Dev

Can't remove Nothing Selected Unity

分類Dev

Can't convert string to ui text in unity

分類Dev

Unity: JDK stop working

分類Dev

Can't stop foreground service stopSelf() and stopForeground(true) not working

分類Dev

Why can't I stop the page-curl animation in UIPageViewController

分類Dev

Can't stop navigation bar covering pushed view - why is it happening?

分類Dev

Can't see controllers in any scene of VRTK using Unity

分類Dev

tcp client can't connect to local server randomly in unity

分類Dev

Android: Coroutine doesn't always work on call

分類Dev

Stop death animation after respawn in Unity

分類Dev

How to Click movement in unity and stop if there is Collider?

分類Dev

How to stop tile animation in Unity TileSystem?

分類Dev

Can function of DAO class be called in ViewModel class without Coroutine

分類Dev

Why can't service host from Unity.Wcf resolve service's instance correctly?

分類Dev

How can I get Unity to recognize installed applications it doesn't list?

分類Dev

Unity、CoroutineはAndroidで異なる動作をします

分類Dev

docker container don't stop

分類Dev

Light isn't flashing in Unity

分類Dev

Generic <T> Unity C#

分類Dev

How can I stop a CSS keyframe animation?

分類Dev

How can I stop grep returning itself

分類Dev

How can I stop grep returning itself

分類Dev

How can I stop this looping statement

分類Dev

How can I catch an exception in Kotlin coroutine when I am awaiting it in another function?

Related 関連記事

  1. 1

    Can't get componentDidUpdate() stop looping

  2. 2

    Can't stop the timer. JavaScript

  3. 3

    jquery scrollTop slideDown can't stop repeat

  4. 4

    apache2 can't stop and nginx can't start

  5. 5

    Why can't i log in on Unity?

  6. 6

    Can't remove Nothing Selected Unity

  7. 7

    Can't convert string to ui text in unity

  8. 8

    Unity: JDK stop working

  9. 9

    Can't stop foreground service stopSelf() and stopForeground(true) not working

  10. 10

    Why can't I stop the page-curl animation in UIPageViewController

  11. 11

    Can't stop navigation bar covering pushed view - why is it happening?

  12. 12

    Can't see controllers in any scene of VRTK using Unity

  13. 13

    tcp client can't connect to local server randomly in unity

  14. 14

    Android: Coroutine doesn't always work on call

  15. 15

    Stop death animation after respawn in Unity

  16. 16

    How to Click movement in unity and stop if there is Collider?

  17. 17

    How to stop tile animation in Unity TileSystem?

  18. 18

    Can function of DAO class be called in ViewModel class without Coroutine

  19. 19

    Why can't service host from Unity.Wcf resolve service's instance correctly?

  20. 20

    How can I get Unity to recognize installed applications it doesn't list?

  21. 21

    Unity、CoroutineはAndroidで異なる動作をします

  22. 22

    docker container don't stop

  23. 23

    Light isn't flashing in Unity

  24. 24

    Generic <T> Unity C#

  25. 25

    How can I stop a CSS keyframe animation?

  26. 26

    How can I stop grep returning itself

  27. 27

    How can I stop grep returning itself

  28. 28

    How can I stop this looping statement

  29. 29

    How can I catch an exception in Kotlin coroutine when I am awaiting it in another function?

ホットタグ

アーカイブ