Skip to content

A collection of write inheritable Singleton scripts for Unity

Notifications You must be signed in to change notification settings

Comp3interactive/Unity-Singletons

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Unity-Singletons

A collection of inheritable Singleton scripts for Unity

How to use
Simply inherit any of your scripts from either Singleton<T> or SingletonDontDestroy<T> and that script will automatically inherit it's own instance and be made available as a singleton

Inheriting from Singleton<T> will simply make the object a singleton whereas inheriting from SingletonDontDestroy<T> will ensure the object persists between scenes by adding it to the DontDestroyOnLoad list.

Examples
Inheriting from the given classes

public class ScoreManager : Singleton<ScoreManager>
{
    private int score;
    public void AddScore(int i)
    {
        score += i;
    }
 }
public class ScoreManager : SingletonDontDestroy<ScoreManager>
{
    private int score;
    public void AddScore(int i)
    {
        score += i;
    }
 }

Calling the classes instance from elsewhere in your project

public class Enemy
{
    private int scoreForKill = 100;
    public void OnKill()
    {
        ScoreManager.instance.AddScore(scoreForKill);
    }
}

About

A collection of write inheritable Singleton scripts for Unity

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages