Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug OnApplicationQuit is NOT called on disabled GameObjects !!

Discussion in 'Scripting' started by Eugenio, Nov 19, 2020.

  1. Eugenio

    Eugenio

    Joined:
    Mar 21, 2013
    Posts:
    197
    Ciao forum !!! :)
    As per thread title. I'm using Unity 2020.1.13f1.

    Please, take a look at the attached Unity project.

    Why this is an issue? I have gameobjects unsubscribing themselves from events in their OnDestroy implementation, although, if they receive OnApplicationQuit first, they will not execute the deregistration, although, the disabled gameobjects run OnDestroy but don't receive OnApplicationQuit. This is causing a multitude of null reference exceptions when the Editor is stopped.

    Could someone from Unity Technologies confirm if this is a bug or an intended behaviour?
    It's hard to believe that this is an intended behaviour: if you receive OnDestroy on disabled gameobjects why not OnApplicationQuit so to avoid the OnDestroy code if necessary?
    Maybe the bug is that disabled gameobjects are receiving the OnDestroy message... I don't know but, to me, there is definitely something wrong here !!

    If it's a bug I'm happily going to submit a bug report.
     

    Attached Files:

  2. NOT_A_ROBOT1101

    NOT_A_ROBOT1101

    Joined:
    Jun 18, 2020
    Posts:
    39
    I'm kinda new to Unity but I think this is an intended feature since (as far as I know) you can't run code on disabled GameObjects.
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    That is definitely not how disabled GameObjects work. Disabling a GameObject will simply cause its components not to receive invocation of some magic methods (Start, Update, FixedUpdate, ...) but most definitely you can run methods on scripts attached to a disabled GameObject.

    OP does seem to be describing unintentional behaviour.
     
  4. Marcurion

    Marcurion

    Joined:
    Jul 29, 2015
    Posts:
    13
    Can confirm this and dates back to at least 2019.4, the issue I see is in consistency since OnDestroy is called on inactive hierachies. I wanted to use it in tandem with OnApplicationQuit to distinguish gameobject deletion from application shutdown...

    Well workaround is to register your function at Application.quitting in e.g. Awake, which gets executed even when inactive.
     
    Last edited: Feb 3, 2022