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.
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");
}
}
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.
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.