Search Unity

[2018.3.1f1] Destroy doesn't immediately set components to NULL

Discussion in 'Scripting' started by Menion-Leah, Jan 15, 2019.

  1. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    I just upgraded from 2018.2 to 2018.3, and I'm experiencing massive issues.

    The root of my problems seems to be related to a change in the way OnDestroy() works: it now no longer set Component/GameObject to 'null' immediately.

    Is anybody experiencing the same situation?
    Thanks
     
    Last edited: Jan 15, 2019
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
  3. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    As far as I experienced, it was destroyed at the end of the Update loop (and OnDestroy was called as well at the same moment), but if you checked:

    Code (CSharp):
    1. gameObject == null
    It used to return true right after Destroy(gameObject) was called.
    Now it doesn't. :/
     
  4. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    I believe this is the expected behavior.

    When you call Destroy on a Unity object that object isn't actually destroyed instantly. Instead the object is simply flagged for destruction and will be disconnected between the current frame and the next. One of the main reasons I believe is for Stability. By only flagging it, it makes it a lot simpler to code classes, and not worry if a valid reference suddenly becomes a null reference later in the same function. Unity can simply unload the resource on it's timetable, in-between the frames where your code would usually run in. Thus your code remains consistent.

    If you Destroy a Gameobject, that Gameobject is still completely functional and safe to work on for the rest of the frame, With the OnDisable/OnDestroy calls happening on the next frame. I've written classes using this expectation ever since I started developing in Unity, back in 5.3.5.

    To say that it wasn't doing this on 2018.2... I would call THAT a bug.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I would venture a wild guess that you're actually encountering a change in the order that your Update and other similar methods are called compared to previous versions. I've discovered a few bugs in my own code whenever this happens that I had just lucked out on through fortunate code execution order previously.
     
  6. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    Sorry for the delay!

    You are right, that is the expected behavior.
    I've been fooled by another issue: the problem was not the gameObject being detected as 'null' or not, it was about colliders.

    Usually, when you delete a GameObject, its Colliders are set to .enabled = false
    With 2018.3.1f1, they weren't.. or, at least, it has been a temporary issue: by opening the project using Unity 2018.2.17f1 and then again with 2018.3.1f1, it "magically" disappeared.

    Thank you for your time