SingletonBase.cs Reusable Singleton Pattern for Unity
SingletonBase.cs β A Reusable Singleton Pattern for Unity
Made with whimsy by Whimsy Droid π€π«
π« Download the ready-to-use Unity package β or tip to support Whimsy Droidβs future creations!
A lightweight, generic Singleton pattern for Unity that ensures only one instance of a manager or system exists at a time.
It automatically handles duplicates, optional scene persistence, and lazy initialization β perfect for Game Managers, Audio Managers, and other global controllers.
β¨ Features
Generic and reusable β works for any MonoBehaviour type
- Thread-safe lazy access pattern
- Auto-creates an instance if missing
- Optional βDonβt Destroy On Loadβ behavior
- Automatically removes duplicates
π§° How to Use
1οΈβ£ Add SingletonBase.cs to your project (or import the included Unity package).
2οΈβ£ Create a new class that inherits from it, for example:
public class AudioManager : SingletonBase<AudioManager>
{
public void PlayClick() => Debug.Log("Click Sound Played");
}
3οΈβ£ Access your manager from anywhere:
AudioManager.Instance.PlayClick();
4οΈβ£ (Optional) Enable βDonβt Destroy On Loadβ in the Inspector to keep it between scenes.
π§ Example
public class GameManager : SingletonBase<GameManager>
{
public int CurrentScore { get; private set; }
Β
public void AddScore(int value)
{
CurrentScore += value;
Debug.Log($"Score updated: {CurrentScore}");
}
}
Use it like this:
GameManager.Instance.AddScore(100);
π¦ Whatβs Included
- SingletonBase.cs β Source code
- SingletonBase.unitypackage β Ready-to-import Unity package
β Tested with Unity 2021 LTS and newer
π§ License: MIT β free for personal and commercial use
π Recharge the Droid
Your support keeps Whimsy Droid coding, compiling, and beeping happily!
- β Ko-fi β send a quick energy boost
- π Patreon β behind-the-scenes & early access
- π» GitHub Sponsors β power up development directly
Join the Droid Network
Connect with other curious devs, catch new tools, and see cute robots in action!
- πΊ YouTube
- πΈ Instagram
- π¦ X (Twitter)
- π΅ TikTok
- π Facebook
π Letβs Build Together!
Whimsy Droid loves hearing from fellow developers π€π
If you have feedback, ideas, or a tool youβd love to see next β let us know!
Your suggestions might inspire the next Whimsy Droid creation.
Reach out on GitHub or say hi on Ko-fi / Patreon.
Letβs make game dev a little cuter β and a lot easier β together π«
π€ From Whimsy Droid with love β tiny cute tools for big developer dreams.