Search Unity

OnDisable order of children when destroying a GameObject

Discussion in 'Scripting' started by MFG-jkhoo, Apr 11, 2017.

  1. MFG-jkhoo

    MFG-jkhoo

    Joined:
    Jul 15, 2016
    Posts:
    19


    Let's say that there are two root GameObjects in a scene: Foo1 and Foo2. Foo1 has nested child GameObjects Bar1 and Bar2. All four GameObjects have a simple empty MonoBehaviour attached to them.

    If I call GameObject.Destroy on Foo1 and then GameObject.Destroy on Foo2, what's the expected order of OnDisable calls for the four GameObjects?

    Code (CSharp):
    1. GameObject.Destroy(GameObject.Find("Foo1"));
    2. GameObject.Destroy(GameObject.Find("Foo2"));
    I'm seeing:
    1. Foo1.OnDisable
    2. Bar1.OnDisable
    3. Foo2.OnDisable
    4. Bar2.OnDisable

    Is this the anticipated behavior? Why isn't all of Foo1 and its children disabled before GameObject.Destroy(GameObject.Find("Foo1") returns? Why is Bar1 disabled but not Bar2?

    I'm trying to determine if this is
    1. Defined behavior - OnDisable behavior is documented somewhere.
    2. Undefined behavior - Unity makes no promises about OnDisable order and does not promise that child GameObjects are disabled before GameObject.Destroy returns.
    3. This is a bug in Unity.