diff --git a/README.md b/README.md index 8c4d6bc..98bf9bc 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,13 @@ Add `using UnityCommunity.UnitySingleton;` for using the classes. - Use `MonoSingleton` if you want a Scene-based singleton which is not persistent across scenes. - Use `PersistentMonoSingleton` if you want a Global and persistent singleton across scenes. +## Common Issues + +1. "Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)" + This issue is caused when trying to unsubscribe from an event in OnDestroy/OnDisable. It happens because the singleton is destroyed before trying to unsubscribe, which spawns a new one as the scene is getting disabled, producing this error. + + To fix this error, check the `.HasInstance` bool on the singleton in an if statement before unsubscribing from events. + ## Contribute Please refer to [CONTRIBUTING.md](CONTRIBUTING.md) for more information on how to contribute to this project. diff --git a/Runtime/Scripts/MonoSingleton.cs b/Runtime/Scripts/MonoSingleton.cs index d4bdd7b..8f10c78 100644 --- a/Runtime/Scripts/MonoSingleton.cs +++ b/Runtime/Scripts/MonoSingleton.cs @@ -51,6 +51,11 @@ public static T Instance return instance; } } + + /// + /// Gets whether an instance of this singleton exists. + /// + public static bool HasInstance => instance != null; /// /// Gets whether the singleton's instance is initialized. diff --git a/Runtime/Scripts/Singleton.cs b/Runtime/Scripts/Singleton.cs index 4766c1e..15a72a4 100644 --- a/Runtime/Scripts/Singleton.cs +++ b/Runtime/Scripts/Singleton.cs @@ -55,6 +55,11 @@ public static T Instance } } + /// + /// Gets whether an instance of this singleton exists. + /// + public static bool HasInstance => instance != null; + /// /// Gets whether the singleton's instance is initialized. ///