Unityのコルーチンでハマった!

こんにちは。ヤマヤタケシです。
まじかよ!
Updateと連動しているだけだと思っていたのに。
というか、勝手に思い込んでいた!

[Unity] コルーチンがなぜか途中で停止するときの確認箇所 」に書いてあるとおりですが、ログを出して確認してみました。
Unity2017.2.1です。

いきなり、まとめます!
1. gameObject.SetActive(false)をするとコルーチンが止まる。
2. gameObject.SetActive(true)をしてもコルーチンは再開しない。

test:Start
test:coroutine 1
test:update 1
test:update 2
test:coroutine 2
test:update 3
test:coroutine 3
test:update 4
test:coroutine 4
test:SetActive. child is Not Active
test:SetActive. child is Active
test:update 11
test:update 12
test:update 13

おまけにコードです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CoroutineTest : MonoBehaviour
{
    void Start()
    {
        Debug.Log("test:Start");
         while (true)
        {
            Debug.LogFormat("test:coroutine {0}", Time.frameCount);
            yield return null;
        }
    }

    void Update()
    {
        Debug.LogFormat("test:update {0}", Time.frameCount);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActiveSwitch : MonoBehaviour
{
    public GameObject child;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)
            || Time.frameCount == 5
            || Time.frameCount == 10)
        {
            child.SetActive(!child.activeSelf);
            Debug.LogFormat("test:SetActive. child is {0}", child.activeInHierarchy ? "Active" : "Not Active");
        }

    }
}

そんじゃまた。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です