Search Unity

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

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

Thread Status:
Not open for further replies.
  1. Discipol

    Discipol

    Joined:
    May 6, 2015
    Posts:
    83
    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:
    129
    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:
    83
    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:
    2
    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
     
    alikun, crandellbr and Discipol like this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
Thread Status:
Not open for further replies.