Skip to content

Instantly share code, notes, and snippets.

@tonymtz
Created October 3, 2016 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonymtz/63c5fbb3e0abce59eb63943835549e06 to your computer and use it in GitHub Desktop.
Save tonymtz/63c5fbb3e0abce59eb63943835549e06 to your computer and use it in GitHub Desktop.
Singleton Unity C#
public class MySingleton : MonoBehaviour
{
private static MySingleton instance;
public static MySingleton Instance
{
get
{
if (instance == null)
{
GameObject mySingletonObject = new GameObject("MySingletonObject");
DontDestroyOnLoad(mySingletonObject);
instance = mySingletonObject.AddComponent<MySingleton>();
}
return instance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment