0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

孫のOnDisableはDestroy直後に呼ばれない

Posted at

ちょっと意外な挙動だったのでメモ。
次のようなGameObjectがあったとして…
Screenshot 2023-01-13 at 19.11.52.png
親であるGameObject (0)をDestroyすれば5つ全てのGameObjectでOnDisable()が呼ばれるが、GameObject (0)GameObject (1)はDestroy直後に呼ばれるのに対し、GameObject (2)以下ではちょっと後になってから呼ばれる。

Screenshot 2023-01-13 at 19.27.26.png

public class Main : MonoBehaviour
{
    public GameObject go;

    void Start()
    {
        Debug.Log($"{Time.realtimeSinceStartup}: ---- Before Destroy ----");
        Destroy(go);
        Debug.Log($"{Time.realtimeSinceStartup}: ---- After Destroy ----");
    }
}

public class OnDisableLogger : MonoBehaviour
{
    void OnDisable()
    {
        Debug.Log($"{Time.realtimeSinceStartup}: {name}.OnDisable");
    }
}
環境

Unity 2021.3.16 LTS
Unity 2020.3.43 LTS

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?