簡體   English   中英

UNITY2D:從另一個腳本運行 CoRoutine 時遇到問題

[英]UNITY2D: Trouble running a CoRoutine from another script

我在腳本上寫了一個 function,當玩家失去所有生命時激活。 這會在附加到我的主要角色的腳本中調用一個 CoRoutine,它會導致簡單的死亡 animation,然后移動到游戲結束屏幕。 Debug.Log顯示調用了function,而當我使用非CoRoutine函數附加到主字符時,那些函數調用到了。 但是,協程本身從不調用,甚至不顯示它曾經激活的日志? 有誰知道發生了什么事?

代碼如下:

if (GameObject.Find("Heart 1") == null)
        {
            Debug.Log("Naw");
            player.DeathAnimation(20);
            Debug.Log("still not working");
        }


public IEnumerator DeathAnimation(int i)
    {
        int k = i;
        Debug.Log("Numerator works");
        transform.Rotate(Vector3.forward * 9);
        yield return new WaitForSeconds(.08f);
        k--;
        Debug.Log(k);
        if (k <= 0)
        {
            SceneManager.LoadScene("Game Over");
            yield break;
        }
        StartCoroutine(DeathAnimation(k));
    }

似乎沒有理由將其設為遞歸協程。 我建議刪除遞歸,這也可能解決您遇到的問題或至少使其更易於識別。

    if (GameObject.Find("Heart 1") == null)
    {
        Debug.Log("Hmm");
        StartCoroutine( player.DeathAnimation(20) );
        Debug.Log("maybe working");
    }

……

public IEnumerator DeathAnimation(int i)
{
    Debug.Log("Numerator works");
    for( ; i>0; i-- ) {
        transform.Rotate(Vector3.forward * 9);
        yield return new WaitForSeconds(.08f);
        Debug.Log(i);
    }
    SceneManager.LoadScene("Game Over");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM