The way I did it was to create a central MusicPlayer object that exists in every scene pretty much. Then there’s a script on it that makes it a singleton, checks if another one exists, etc.
Then I made another script (on a separate, central object) that checks for the MusicPlayer’s current track. On this script is where you’d attach your current track. It checks if the assigned track is the same as the MusicPlayer’s current track. If it is, leave it alone. If it isn’t, play the assigned track.
Basically, the cause of it reloading is that the MusicPlayer is destroyed and recreated every scene, which causes the music to start from the beginning. This method takes care of that in every way.
By the way, if you want the two scripts (skimmed a bit to take off a few specific modifications [maybe if I don’t get lazy] to simplify it), I’d be happy to as I DID find the basics on the Unity Answers (after quite a bit of searching) and finally, with some modifications, settled on the above method.
Edit: Oh, and note that the MusicPlayer singleton has DontDestroyOnLoad() so it persists between scenes smoothly (which is why you’d need a singleton and all- I guess the rest of the details were just the little details to get it working as expected )
Make the MusicPlayer DontDestroyOnLoad so it doesn’t get destroyed between scenes
So there aren’t multiple copies and memory leaks, use a script to make the MusicPlayer a “singleton” and check if there is already an instance existing when switching scenes
Use another script on a different object (I have a central Game Object called ControlCenter where this and another central script reside) that controls the music (and I extended it to playing sound effects). This is the script where you would specify which music you’d want. It also checks if the current music is already playing, and WON’T replay it if it’s the same track. However, if they’re different, it’ll play the new BGM instead. This is necessary as the MusicPlayer will exist between levels and you will need a way to change the track for the “permanent” object.
Here are the two scripts:
MusicPlayer.cs (the actual MusicPlayer object’s script)
using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour {
public static MusicPlayer instance = null;
public static MusicPlayer Instance {
get { return instance; }
}
void Awake() {
if (instance != null instance != this) {
Destroy(this.gameObject);
return;
} else {
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
}
Here’s the Control_MusicManager.cs script (the one that controls the music)
using UnityEngine;
using System.Collections;
public class Control_MusicManager : MonoBehaviour {
// The actual Music Player object
GameObject MPlayer;
// The assigned BGM from Inspector
public AudioClip newMusic;
// Miscelleneous SFX for multiple uses
public AudioClip boosterSound;
public AudioClip buttonSound;
public AudioClip clickSound;
public AudioClip deathSound;
public AudioClip jBoosterSound;
public AudioClip menuSound;
public AudioClip pickupSound;
public AudioClip secretSound;
public AudioClip winSound;
// ...This could be more efficient
void Start () {
if( MPlayer == null)
MPlayer = MusicPlayer.instance.gameObject;
if(newMusic != null){
if(MPlayer.audio.clip != newMusic){
MPlayer.audio.clip = newMusic;
MPlayer.audio.Play ();
}
}
}
public void BoosterSound(){
MPlayer.audio.PlayOneShot(boosterSound,1.5f);
}
public void JBoosterSound(){
MPlayer.audio.PlayOneShot(jBoosterSound, 1.5f);
}
public void ButtonSound(){
MPlayer.audio.PlayOneShot(buttonSound);
}
public void ClickSound(){
MPlayer.audio.PlayOneShot(clickSound,1.5f);
}
public void DeathBell(){
MPlayer.audio.PlayOneShot(deathSound);
}
public void MenuClick(){
MPlayer.audio.PlayOneShot(menuSound);
}
public void PickupSound(){
MPlayer.audio.PlayOneShot(pickupSound);
}
public void SecretSound(){
MPlayer.audio.PlayOneShot(secretSound,2);
}
public void WinSound(){
MPlayer.audio.PlayOneShot(winSound);
}
}
Hope that helps! Please tell me if it works as is, or if you need anything else. Don’t have time to really convert these to UnityScript right now though.