SceneManager.sceneUnloaded gets called in editor when selecting materials

Bug report case 946988
Edit - QA team has reproduced the bug and sent it for resolution.

There seems to be a bug with SceneManager.sceneUnloaded where it gets called in the editor during play mode while selecting materials in the project view, or even just selecting a gameobject with a MeshRenderer that has materials in the material list. Even selecting Prefabs in the project view that have a MeshRenderer with materials will cause the sceneUnloaded to be called.

This is breaking game code as some code relies on sceneUnloaded to just be called when the scene is unloaded.

Here is a video demonstration
Click for video

Tested in Unity 2017.1.0p4 (64-bit)

Here is the test code
Click for code

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TestSceneUnloadedBug : MonoBehaviour
{
    void Awake()
    {
        SceneManager.sceneLoaded += SceneLoaded;
        SceneManager.sceneUnloaded += SceneUnloaded;
    }

    public void SceneLoaded(Scene scene, LoadSceneMode mode)
    {
        Debug.Log("Scene has loaded");
    }

    public void SceneUnloaded(Scene scene)
    {
        Debug.Log("Scene has unloaded");
    }
}
3 Likes

Just hit this today. I have a pool manager that listens to Scene unload. Was wondering why my objects were magically being destroyed.

I just ran into this problem in Unity 2017.1.2. Has this been fixed yet?

It has in 2017.2.0f3, but I then ran into another strange issue (in 2017.2.0f3 at least) with when I had the inspector in Debug mode, for some reason -I think- a singleton object I had that had a [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] attribute on it would cause the onSceneLoad event to be called, and then the onSceneUnload event gets called after it, when the scene just finished loading…
I tried for a little to reproduce, but I just gave up and stopped using the sceneUnload event and just put whatever code I wanted to run onSceneUnload into the code that gets ran at onSceneLoad before anything else. =/

Thanks. I updated to Unity 2017.2.0f3 and unfortunately this is still causing me problems. Really odd issue to be having. Everytime I inspect anything with a material on it, the sceneUnload event gets called.

Use this hotfix for that issue

Edit:

  • It’s only fix for material, sprite, but when select to particle, it’s still bug

In case the bug isn’t being monitored, I’m seeing this in 2017.3.0f3. It isn’t completely clear which version the ā€œhotfixā€ went into. Also no link to the hotfix here or in the issue that I could find. (sorry if I’m being blind)

I’m also seeing sceneUnloaded being called when it shouldn’t be. One is it’s trying to unload ā€˜Preview Scene’, which sounds like it might be related to the selecting materials thing. But I’m also seeing it try to unload the current scene when I hit the space key, which I’m guessing is some third party asset that’s doing the wrong thing. But in any case this doesn’t seem like a good event to be relying on for anything critical.