//Set the instance to null ondestroy void OnDestroy() { if (Instance == this) { Instance = null; } }
void Awake() { //Check if there is already an instance of if (Instance == null) { //if not, set it to this. Instance = this; DontDestroyOnLoad(gameObject); } //If instance already exists: else if (Instance != this && Instance != null) { //Destroy this, this enforces our singleton pattern. Destroy(gameObject); } }