Last active
December 3, 2015 02:56
-
-
Save Ashwinning/96e63742ca52bfff94a3 to your computer and use it in GitHub Desktop.
How to use Lazy Singletons in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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