Skip to content

Instantly share code, notes, and snippets.

@Ashwinning
Last active December 3, 2015 02:56
Show Gist options
  • Save Ashwinning/96e63742ca52bfff94a3 to your computer and use it in GitHub Desktop.
Save Ashwinning/96e63742ca52bfff94a3 to your computer and use it in GitHub Desktop.
How to use Lazy Singletons in C#
public class MusicManager : MonoBehaviour
{
//We make a static variable to our MusicManager instance
public static MusicManager instance { get; private set; }
//When the object awakens, we assign the static variable
void Awake()
{
instance = this;
}
public void Play()
{
//Play some audio!
}
}
//...
//Now in another class, we can call Play() by using the static variable!
public class LevelController : MonoBehaviour
{
void PlayMusic()
{
MusicManager.instance.Play();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment