So I’m just having one of those mornings, not quite grasping the best way around this.
My singleton instance invokes events to which other things subscribe. Subscribers subscribe to those events through the singleton’s instance.
When I exit play mode, my subscribers call their OnDisable methods to unsubscribe, but evidently the singleton instance has already been destroyed by this point, so a new instance is created.
I figured I’d just add a DontSave hideflag to the singleton instance when it is lazy-created, but the object persists once play mode has been exited. Therefor I end up with errors immediately (before playmode has fully exited) because of null references, and must delete the unwanted instance from the scene.
I know there’s gotta be a clever way I can still use this design pattern, but I’m not caffeinated enough to see it. Any suggestions would be great!
ps: my singletons are monobehaviors to conveniently expose them to the inspector
Why can't you just check whether this singleton is null in OnDisable method of the subscriber when trying to unsubscribe to avoid such errors?
– MrGuardianXThanks for the input, detailed comment on @HarshadK 's answer. :)
– AlwaysSunny