From e0aecb7517388f5d36f73aaf26ac316e80393576 Mon Sep 17 00:00:00 2001 From: mydimons Date: Fri, 22 May 2026 13:50:48 -0700 Subject: [PATCH 1/3] Add "HasInstance" boolean The HasInstance boolean checks if an instance of the singleton exists without spawning a new singleton. Fixes an issue with events when trying to unsubscribe in OnDestroy/OnDisable in #17 --- Runtime/Scripts/MonoSingleton.cs | 5 +++++ Runtime/Scripts/Singleton.cs | 5 +++++ 2 files changed, 10 insertions(+) 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. /// From 37fd9cd471fe5b168afc879940556d1fc0eaaae2 Mon Sep 17 00:00:00 2001 From: mydimons Date: Fri, 22 May 2026 13:58:34 -0700 Subject: [PATCH 2/3] Update README.md with common issues --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 8c4d6bc..7c266f7 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, and because no singleton exists it tries to spawn a new one. It spawns this new singleton 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 the events. + ## Contribute Please refer to [CONTRIBUTING.md](CONTRIBUTING.md) for more information on how to contribute to this project. From 3e64330bd315601ee1198331aa1b017d4b1313f0 Mon Sep 17 00:00:00 2001 From: mydimons Date: Fri, 22 May 2026 14:05:56 -0700 Subject: [PATCH 3/3] README.md, shortened common issues section --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7c266f7..98bf9bc 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,9 @@ Add `using UnityCommunity.UnitySingleton;` for using the classes. ## 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, and because no singleton exists it tries to spawn a new one. It spawns this new singleton as the scene is getting disabled, producing this error. + 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 the events. + To fix this error, check the `.HasInstance` bool on the singleton in an if statement before unsubscribing from events. ## Contribute