void Awake() { if (instance != null && instance != this) { Destroy(this.gameObject); return; } else { instance = this; } DontDestroyOnLoad(this.gameObject); }
/// <summary> /// Use singleton pattern to keep music playing between scenes. /// </sumnary> void Awake() { if (instance != null && instance != this) { Destroy(this.gameObject); return; } else { instance = this; } DontDestroyOnLoad(this.gameObject); /* Keep music playing on a not beauty way. Change it to use singleton in the future! * GameObject[] objs = GameObject.FindGameObjectsWithTag("Music"); * if(objs.Length > 1) * Destroy(this.gameObject); * DontDestroyOnLoad(this.gameObject); */ }