The Singleton pattern is commonly used in Unity for systems that should have one globally accessible instance.
Typical examples include:
- GameManager
- AudioManager
- SaveManager
- SettingsManager
A basic implementation is:
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
}
Other scripts can then access:
GameManager.Instance
Don’t Use Singleton Everywhere
The convenience of:
Something.Instance
can become a problem if every system uses it.
Excessive Singleton usage can create:
- Hidden dependencies
- Global state
- Difficult testing
- Tight coupling
For larger projects, consider:
- Dependency injection
- Interfaces
- Events
- Service containers
- Composition
Conclusion
Singleton is useful when a system genuinely requires one shared instance.
Use it intentionally rather than automatically.