Script for controlling background music for different scenes.
Inheritance: MonoBehaviour
Example #1
0
 // Creates singleton on first run.
 void Awake()
 {
     if (instance == null)
     {
         instance = this;
         this.playingMusic = GetComponent<AudioSource>();
         this.playingMusic.ignoreListenerVolume = true;
         DontDestroyOnLoad(this);
     }
     else
     {
         // Self-destruct if another instance exists
         Destroy(this);
         return;
     }
 }
 // Creates singleton on first run.
 void Awake()
 {
     if (instance == null)
     {
         instance          = this;
         this.playingMusic = GetComponent <AudioSource>();
         this.playingMusic.ignoreListenerVolume = true;
         DontDestroyOnLoad(this);
     }
     else
     {
         // Self-destruct if another instance exists
         Destroy(this);
         return;
     }
 }
 // Uses MusicControlScript (BGM music script) to change the volume of the
 // background music.
 private void ChangeMusicVolume()
 {
     MusicControlScript.ChangeMusicVolume(musicSlider.value);
     PlayerPrefs.SetFloat("musicVolume", musicSlider.value);
 }