- Home /
User error
Call Method on Scene Load without SceneManager.sceneLoaded
Recently I refactored some of my UI elements so they exist across scenes via DontDestroyOnLoad. I have a level progress meter that I need to refresh at the beginning of each scene. The problem is, subscribing to sceneLoaded is calling my refresh method before MonoBehaviour has even been initialized so I'm running into NullReferenceExceptions for base components like this:
NullReferenceException at (wrapper managed-to-native) UnityEngine.Behaviour.get_isActiveAndEnabled(UnityEngine.Behaviour)
Is there an alternative I can use? All I want is to call my method after the scene has loaded.
Can you show some code? It might be that you use sceneLoaded method on your old (now deleted) object if you use DontDestroyOnLoad as a singleton, or something.
Here is the doc: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html
This morning I realized I had actually moved that UI back out of DontDestroyOnLoad, so that's why it was happening before Behaviour was ready. Thanks for the comment, it helped me realize what was going wrong
Follow this Question
Related Questions
How do I change scenes from triggers in UNity 2 Answers
Loading Screen? 6 Answers
Loading a level and getting all new gameobjects 1 Answer
Old Scene still visible after Loading new 0 Answers
Loading New Scene & Graphics Problem 0 Answers