Unity 5.4.1 OnLevelWasLoaded Workaround for Singletons?

I have some singletons in my project that used OnLevelWasLoaded functions.

I’ve seen some workarounds but not for Singletons that have OnLevel…
Like this One: (NOT MINE)

void OnEnable()
             {
              //Tell our 'OnLevelFinishedLoading' function to start listening for a scene change as soon as this script is enabled.
                 SceneManager.sceneLoaded += OnLevelFinishedLoading;
             }
         
             void OnDisable()
             {
             //Tell our 'OnLevelFinishedLoading' function to stop listening for a scene change as soon as this script is disabled. Remember to always have an unsubscription for every delegate you subscribe to!
                 SceneManager.sceneLoaded -= OnLevelFinishedLoading;
             }
         
             void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
             {
                 Debug.Log("Level Loaded");
                 Debug.Log(scene.name);
                 Debug.Log(mode);
             }

The problem is that, It’s called several times, in scenes that it does not need to be called.
What I need is to be able to call the “OnLevelFinishedLoading” function, only in certain scenes, not in ALL of them, that is the problem with the code shown above, it’s designed for NON singleton gameobjects.

Any workaround for the Singleton gameobjects?

Thanks in advance!
-Chris

You are using an event that is triggered on every scene, so your function will be called on every scene, what you need is to write a custom event, so you can make your function listen to it instead of listening to sceneLoaded, then you can just choose which scenes to trigger the event :slight_smile:

You can just check if the scene is the one you want:

 void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
    {
           if(scene.name == "MyScene"){
               //Do stuff
           }
    }