Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

SceneManager.sceneUnloaded gets called in editor when selecting materials

Discussion in 'Editor & General Support' started by HiddenMonk, Aug 31, 2017.

  1. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    974
    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

    Tested in Unity 2017.1.0p4 (64-bit)

    Here is the test code
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class TestSceneUnloadedBug : MonoBehaviour
    6. {
    7.     void Awake()
    8.     {
    9.         SceneManager.sceneLoaded += SceneLoaded;
    10.         SceneManager.sceneUnloaded += SceneUnloaded;
    11.     }
    12.  
    13.     public void SceneLoaded(Scene scene, LoadSceneMode mode)
    14.     {
    15.         Debug.Log("Scene has loaded");
    16.     }
    17.  
    18.     public void SceneUnloaded(Scene scene)
    19.     {
    20.         Debug.Log("Scene has unloaded");
    21.     }
    22. }
     
    Last edited: Aug 31, 2017
    deetru, CDF and Peter77 like this.
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    711
    Just hit this today. I have a pool manager that listens to Scene unload. Was wondering why my objects were magically being destroyed.
     
  3. deetru

    deetru

    Joined:
    Feb 3, 2015
    Posts:
    5
    I just ran into this problem in Unity 2017.1.2. Has this been fixed yet?
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    974
    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. =/
     
  5. deetru

    deetru

    Joined:
    Feb 3, 2015
    Posts:
    5
    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.
     
  6. JimHL

    JimHL

    Joined:
    Jul 17, 2017
    Posts:
    8
    Use this hotfix for that issue

    Edit:
    - It's only fix for material, sprite, but when select to particle, it's still bug
     
    Last edited: Dec 13, 2017
  7. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    558
    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)
     
  8. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,107
    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.