I have a SingletonManager class with an instance of Singleton (and an interface to it). GameObject 'LevelManager' does not have to be stable throughout the entire application life cycle and should be replaced with a new instance each time a new level is loaded.
When I run my current code, when the scene loads a second time, the "LevelManager" seems to be missing, even though the GameObject (with the script application) is in the scene. I understand that this is probably because I'm still trying to access the old link.
What is the best way to ensure that my "SingletonManager" stores a new instance of "LevelManager" every time a new level is loaded?
My code (inappropriately deleted):
SingletonManager:
public class SingletonManager { private static LevelManager instance; public static ILevelManager Instance { get { if (this.levelManager == null) { this.levelManager = GameObject.FindObjectOfType(typeof(LevelManager)) as LevelManager; } return this.levelManager; } } }
Singleton is not permanent:
public class Singleton<Instance> : MonoBehaviour where Instance : Singleton<Instance> { public static Instance instance; public bool isPersistant; public virtual void Awake() { if(isPersistant) { if(!instance) { instance = this as Instance; } else { DestroyObject(gameObject); } DontDestroyOnLoad(gameObject); } else { instance = this as Instance; } } }
source share