Your Cart
Loading
Only -1 left

SingletonBase.cs Reusable Singleton Pattern for Unity

On Sale
$0.00
Free Download
Added to cart

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!


πŸ’Œ 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.


You will get the following files:
  • CS (3KB)
  • UNITYPACKAGE (8KB)
  • ZIP (10KB)