You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think a simple way to avoid this is to have a method to check whether there is any existing instance there without calling the get accessor on the Singleton class that initializes a new instance if there aren't any.
You could try it in your local.copy and see how it performs, like add a new method/property that just checks the internal instance field
of the singleton for null, and returns:
publicboolHasInstance=> instance !=null;
This way, when you're unsubscribing for an event if the object is already destroyed, then no new object gets initialized by the singleton, as you're using the HasInstance in place of Instance in OnDestroy.
This should be only intended to be used in such cases as of OnDestroy or OnDisable where the instance it self could be destroyed due to Unity unloading the scene.
I think it's needed to unsubscribe from events in
OnDestroy
to prevent memory leaks, such as:This works, except for when exiting play mode, sometimes it will log:
I believe it's because:
StatsManager
first.StatsManager
event.StatsManager.Instance != null
, it causesStatsManager
to respawn sinceStatsManager.Instance
is null. (Internal Singleton behavior)What's the best way to avoid this issue?
The text was updated successfully, but these errors were encountered: