亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在 Unity 中停止協程

無法在 Unity 中停止協程

C#
HUH函數 2022-11-21 16:58:13
我試圖阻止玩家死亡時產生的波浪,但我似乎無法阻止協程。我真的不知道如何再攻擊它,因為添加 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;    }}
查看完整描述

4 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

  1. 當玩家死亡時,您需要將 gameOver 設置為 True

  2. 您還需要添加邏輯以將 while 循環更改為 false 以退出 while 循環。

例如在 while 循環內設置gameOver = truewhen并設置currentHealth == 0while(gameOver == false)


查看完整回答
反對 回復 2022-11-21
?
MM們

TA貢獻1886條經驗 獲得超2個贊

我認為問題可能是當您調用 StopCoroutine() 時,您并沒有指示它停止協程的特定實例。相反,您需要存儲對協程的引用并將其作為參數傳遞給您的 Stop。


IEnumerator wavecoroutine = SpawnWaves();

StartCoroutine(wavecoroutine);


...


StopCoroutine(wavecoroutine);


查看完整回答
反對 回復 2022-11-21
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

ryeMoss 實際上有正確的答案。您不需要更改 while 循環的條件,而是需要確保將參考值傳遞給該StopCoroutine方法。您可以在文檔中看到這正是他們正在做的事情。


問題是什么時候SpawnWaves調用它返回一個新IEnumerator的,當你試圖阻止它時,這顯然不是你想要的,哈哈。


Start只需在您的方法內部更改為


gameOver = false;

waves = SpawnWaves(); <-- or whatever you want to call it

StartCoroutine(waves); <-- using a reference

然后傳遞waves給StopCoroutine.


始終仔細閱讀文檔;在學習新的庫、框架等時,他們是你最好的朋友。:)


查看完整回答
反對 回復 2022-11-21
?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

完全修復感謝@AlexG 的指導


void Start () {

    gameOver = false;

    StartCoroutine (SpawnWaves());

}


IEnumerator SpawnWaves()

{

    yield return new WaitForSeconds(startWait);

    while (gameOver != true)

    {

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

        {

            if(gameOver)break;

            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 *= eenter code herenemyHealthMultiplier;

    }

}


查看完整回答
反對 回復 2022-11-21
  • 4 回答
  • 0 關注
  • 207 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號