転職を繰り返したサラリーマンの多趣味ブログ

30才未経験でSEに転職した人の多趣味ブログ

【Unity】Coroutine continue failureというエラーの解決法

StopCoroutineでコルーチンを停止させようと実装したら、「Coroutine continue failure」とエラーがでた。
f:id:uuc1h:20210807115211p:plain
で、エラーがでた実装はこんな感じです。

public void GameOver() {

        // リトライボタンとタイトルボタンの表示
        RetryButton.SetActive(true);
        TitleButton.SetActive(true);

        // ブログ用
        StopCoroutine(calculationManager.someCoroutine);

        // ハイスコアのジャッジ
        if (PlayerPrefs.GetInt("HighPingScore", 0) < scoreManager.pingScore) {

            PlayerPrefs.SetInt("HighPingScore", scoreManager.pingScore);

        }

        if (PlayerPrefs.GetInt("HighCalcScore", 0) < scoreManager.rightScore) {

            PlayerPrefs.SetInt("HighCalcScore", scoreManager.rightScore);

        }

    }

色々調べてみると、StopCoroutineの引数に渡しているインスタンスが、外部クラスなことが原因っぽかったです。
なんで、以下のように変更しました。

public void GameOver() {

        // リトライボタンとタイトルボタンの表示
        RetryButton.SetActive(true);
        TitleButton.SetActive(true);

        // 計算のカウントダウンの停止
        calculationManager.StopSomeCoroutine();

        // ハイスコアのジャッジ
        if (PlayerPrefs.GetInt("HighPingScore", 0) < scoreManager.pingScore) {

            PlayerPrefs.SetInt("HighPingScore", scoreManager.pingScore);

        }

        if (PlayerPrefs.GetInt("HighCalcScore", 0) < scoreManager.rightScore) {

            PlayerPrefs.SetInt("HighCalcScore", scoreManager.rightScore);

        }

    }

コルーチンを停止させる動きは、CalculationManagerクラスのStopSomeCoroutineメソッドで行うように修正。で、CalculationManagerクラスのStopSomeCoroutineメソッドは、こんな実装です。

public Coroutine someCoroutine;

public void Init() {

        // カウント開始
        someCoroutine = StartCoroutine(countDownManager.CountDown(false));

    }

public void StopSomeCoroutine() {

        StopCoroutine(someCoroutine);

    }

StopCoroutineに渡す引数は自クラス内で処理するように修正したら、エラーでなくなりました。