LoginSignup
8
4

More than 5 years have passed since last update.

[Unity] Coroutineを実行中のGameObjectをディアクティブにしたときのコルーチンの状態について

Posted at

概要

すでに実行されているコルーチンはGameObjectのSetActive(false)で
どうなるでしょうか?という話。
忘れてしまうので備忘録として書いておきます。

環境

Unity5.5.2p4

実験と結果

下記のオブジェクトをGameObjectにアタッチしてエディタをプレイモードにする。

using UnityEngine;
using System.Collections;

public class TestCoroutine : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(StartLoop());
    }

    IEnumerator StartLoop()
    {
        Debug.Log("[COROUTINE] Start.");
        WaitForSeconds wait = new WaitForSeconds(1f);
        Debug.Log("[COROUTINE] Wait...");
        yield return wait;
        Debug.Log("[COROUTINE] Deactive object.");
        this.gameObject.SetActive(false);

        while(true)
        {
            yield return wait;
            Debug.Log("[COROUTINE] Wait...");
        }
    }
}
ログの出力結果
[COROUTINE] Start.
[COROUTINE] Wait...
[COROUTINE] Deactive object.

エディタ上でスクリプトをアタッチしたGameObjectをアクティブにしても
コルーチンは復帰されない。
非アクティブにしたらコルーチンは消えてなくなるってことですね。

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4