-
Notifications
You must be signed in to change notification settings - Fork 69
Open
Assignees
Labels
@KamilDev
Description
I think it's needed to unsubscribe from events in OnDestroy to prevent memory leaks, such as:
private void OnDestroy() { if (StatsManager.Instance != null) { StatsManager.Instance.OnStatsChanged -= OnStatsChanged; } }
This works, except for when exiting play mode, sometimes it will log:
Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)
I believe it's because:
- Unity destroys
StatsManagerfirst. - Unity next destroys the other GameObject that unsubcribes from
StatsManagerevent. - When checking
StatsManager.Instance != null, it causesStatsManagerto respawn sinceStatsManager.Instanceis null. (Internal Singleton behavior)
What's the best way to avoid this issue?