Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ Add `using UnityCommunity.UnitySingleton;` for using the classes.
- Use `MonoSingleton<T>` if you want a Scene-based singleton which is not persistent across scenes.
- Use `PersistentMonoSingleton<T>` 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 `<T>.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.
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Scripts/MonoSingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public static T Instance
return instance;
}
}

/// <summary>
/// Gets whether an instance of this singleton exists.
/// </summary>
public static bool HasInstance => instance != null;

/// <summary>
/// Gets whether the singleton's instance is initialized.
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Scripts/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public static T Instance
}
}

/// <summary>
/// Gets whether an instance of this singleton exists.
/// </summary>
public static bool HasInstance => instance != null;

/// <summary>
/// Gets whether the singleton's instance is initialized.
/// </summary>
Expand Down