Created
September 3, 2019 21:46
-
-
Save eduardonunesp/7f3aaa2bceb8daaba7bfb84f44ebcdab to your computer and use it in GitHub Desktop.
Singleton Pattern
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class GameManager : MonoBehaviour | |
{ | |
public static GameManager Instance { get { return _instance; } } | |
private static GameManager _instance; | |
void Awake() | |
{ | |
//Check if instance already exists | |
if (_instance == null) | |
{ | |
//if not, set instance to this | |
_instance = this; | |
//If instance already exists and it's not this: | |
} | |
else if (_instance != this) | |
{ | |
//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager. | |
Destroy(gameObject); | |
} | |
//Sets this to not be destroyed when reloading scene | |
DontDestroyOnLoad(gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment