Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. The Burst compiler has its own forum section now.
    Dismiss Notice
  4. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

OnDestroy won't be called on inactive objects, how to workaround this?

Discussion in 'Scripting' started by Discipol, Mar 17, 2020.

  1. Discipol

    Discipol

    Joined:
    May 6, 2015
    Posts:
    68
    Ok SO!
    I am using a framework called PureMVC where a non Monobehavior class called Mediator wraps around a Monobehavior class and contains all business logic. Monobehavior scripts aren't allowed to store data or initiate any action.

    My issue is that a Mediator should wrap around a Monobehavior as long as that Monobehavior exists, and if the Mono is destroyed, the Mediator should die as well.

    This is where OnDestroy should have been a simple solution, but I just found out that, if the Mono script's gameobject is inactive (hidden pages in a book type UI), it won't trigger OnDestroy and a lot of Mediators will still exist but have dead Monobehaviors.

    How do I workaround this? Can't use OnDisable because I turn active/inactive my Monos a lot. I just need to automatically notifiy my Mediator that the view just has died, only once. When the Mediator wraps around the Monobehavior, it can attach some Action events (its how I planed for OnDestroy to tell the Mediator) but I can't find any way of detecting when a inactive Monovehavior is destroyed.

    I thank you in advance!
     
    Last edited: Mar 17, 2020
  2. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    105
    Where do you destroy your objects from? Maybe you should rethink the way that is handled. For example, instead of calling Destroy on the targeted GameObject directly, create a Destroy method for the Mediator that destroys the GameObject it is attached to and then itself. Maybe something along those lines?
     
  3. Discipol

    Discipol

    Joined:
    May 6, 2015
    Posts:
    68
    Some elements I Destroy manually which I would have control. Most of them, however, happen when I change the scene. They are destroyed because the scene is unloaded at whatever hierarchy depth they may be.
     
  4. thebestpronagibator

    thebestpronagibator

    Joined:
    Mar 12, 2019
    Posts:
    1
    I use Awake & OnDestroy to sub/unsub my events (Action += method), but sometime my script subscribe to events and can't unsubscribe coz gameobject isn't inited (turned on) so OnDestroy didnt called. For solve this problem i just add:
    if (!this) return; in my event
     
    Discipol likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    10,244
    On the original question, you could add another Monobehaviour script to the GameObjects which you leave enabled. This Monobehaviour would maintain a list of all Mediator components attached. In its OnDestroy method it iterates through the list of Mediators, and for any which are disabled it does whatever notification is needed. Probably just calling a method on the Mediator to tell it to notify that it is destroyed, or you could even just directly call its OnDestroy method.
     
    Discipol likes this.
unityunity