Search Unity

Does DontDestroyOnLoad Prevent OnDestroy and OnDisable from Triggering On Scene Change?

Discussion in 'Editor & General Support' started by UncleAlias, Jan 26, 2021.

  1. UncleAlias

    UncleAlias

    Joined:
    Aug 18, 2017
    Posts:
    27
    I have a gameobject with a script that sets DontDestroyOnLoad(gameobject), but OnDestroy and OnDisable are still being triggered on another script on that gameobject when a new scene is loaded. So I can't figure out how to only execute code when the gameobject is actually destroyed. I tried subscribing to an event that usually occurs under the same circumstances, but then I couldn't figure out how to reliably unsubscribe to it if not using OnDestroy!

    Does anybody know if this is normal behaviour? The documentation for OnDestroy() does say that it is triggered on scene change, but it doesn't say anything about DontDestroyOnLoad on that page.