Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

DontDestroyOnLoad doesn't preserve state, disabled object is being re-enabled on scene load

Discussion in 'Editor & General Support' started by DreamPower, Sep 4, 2019.

  1. DreamPower

    DreamPower

    Joined:
    Apr 2, 2017
    Posts:
    103
    EDIT:
    I was stupid, my Action function was getting triggered an extra time because it wasn't getting unsubscribed, and that was reactivating my object. Nothing to see here, move along!

    I thought DontDestroyOnLoad just kept objects exactly as they were when a new scene was loaded. But I'm not experiencing that: my object has activeSelf false, then I load a different scene that does not include the object, and the object's OnEnable function is called and the object is made active again. I tried to re-disable it with SceneManager.sceneLoaded, but the enabling is being done after sceneLoaded .

    How can I prevent my disabled object from being enabled, and OnEnable called? Is this intended Unity functionality?

    Unity 2019.2.3
     
    Last edited: Sep 4, 2019