Search Unity

Loading a Scene unloads the previous scene in Editor, but fails to unload it in a (Mac or iOS) Build

Discussion in 'Scripting' started by Slapworth, Sep 22, 2018.

  1. Slapworth

    Slapworth

    Joined:
    Aug 11, 2013
    Posts:
    19
    Hi -

    I've got a situation with my project that's driving me nuts. My scripts for loading and unloading scenes broke at some point, in the following strange ways:
    • First off, the load methods are all pretty simple, using SceneManager to Load single scenes. (No additive scenes.)
    • They work fine on the Editor:
      • I can launch the game from a level menu, select and load a level.
      • I can then return to the main menu via a button,
      • Or I can advance to the next level when I complete the level goals.
    • But on the build, whenever I try to leave a level or advance to a new one, it fails to unload the current level scene, and I end up with two scenes running concurrently.
    • It throws no errors or seemingly relevant warnings in the process.
    • All of this worked fine until sometime in the past several months; I haven't tested a build in a while, so I can't narrow down the timetable closer than that.
    I don't know what's going on, and I'm not familiar enough with debugging on device to be able to tell what's going on there.

    I did confirm that if I strip out MOST of the gameplay in my level, it starts working on at least one platform (Mac). So SOMETHING in my gameplay scripts is causing the issue... but I can't figure out how to track it down.

    So while I start slowly ripping apart the game to look for a culprit, has anyone here even heard of such a problem with scenes not unloading when expected? My googling has led nowhere.

    Thanks - Slapworth
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Just to cover the obvious...
    Are you calling DontDestroyOnLoad() anywhere?
     
  3. Slapworth

    Slapworth

    Joined:
    Aug 11, 2013
    Posts:
    19
    Hi Antistone -

    I am using DontDestroyOnLoad() on one single object (a data collection), but I've been doing that for years and it hadn't caused this issue.

    Did something change in 2017.x that would have changed the protocols for using DontDestroyOnLoad?

    (UPDATE: Also, in the one test level I created that DOES unload correctly, I'm still using DontDestroyOnLoad on that object and it isn't breaking things.

    I only had to delete 99% of my other scripts from the level to make it work!)
     
  4. Slapworth

    Slapworth

    Joined:
    Aug 11, 2013
    Posts:
    19
    UPDATE: I found it! One more DontDestroyFromLoad crept into my project based on a Singleton pattern I copied from somewhere. I think I'm good now.

    PROTIP: Source control is your friend! Once I was able to narrow the issue down to a specific commit, I narrowed the issue down to around 10 files.

    Thanks for pointing me in the right direction, Antistone!
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    A "Find References" or "Find in Files" command can be used to check if there are any references to a specific function or variable that you may have forgotten about. (At least in Visual Studio, but most IDEs probably have similar commands.)
     
    Joe-Censored likes this.
  6. Slapworth

    Slapworth

    Joined:
    Aug 11, 2013
    Posts:
    19
    Thanks! Yeah, I realized that I should have applied Occam's Razor here: if something isn't being destroyed after load, then maybe search my project for DontDestroyOnLoad(). :)
     
    Joe-Censored likes this.