Audio or Music to continue playing between scene

Is there a simple way to have audio like a background music track to carry over to another scene without stopping or restarting? but in some scenes and change the music in other scenes, and Thanks

http://answers.unity3d.com/questions/413203/continuous-music-across-multiple-scenes.html

2 Answers

2

If you call the don’t destroy on load function on your music player, then it won’t be destroyed when you switch between scenes.

The only problem is that if you return to the scene where you started the music you will have two music players. The easy way to fix that is to do a check to see if your music player already exists when you load into that scene.

The below code is untested and surely has mistakes in it, but I tried to comment what I was doing so you could sort of get the idea of the logic involved. You would put this script on your music player. NOTE: The music player would need to be called something other than MUSIC at first for this to work.

EDIT: I just had to actually use this code in my own project and I found some bugs in it so I’m re-posting it here in case someone sees this and is still having the same problem of duplicate music players.

	public GameObject musicPlayer;
	void Awake() {
		//When the scene loads it checks if there is an object called "MUSIC".
		musicPlayer = GameObject.Find("MUSIC");
		if(musicPlayer==null)
		{
			//If this object does not exist then it does the following:
			//1. Sets the object this script is attached to as the music player
			musicPlayer = this.gameObject;
			//2. Renames THIS object to "MUSIC" for next time
			musicPlayer.name = "MUSIC";
			//3. Tells THIS object not to die when changing scenes.
			DontDestroyOnLoad(musicPlayer);
		}else{
            if(this.gameObject.name!="MUSIC"){
			//If there WAS an object in the scene called "MUSIC" (because we have come back to
			//the scene where the music was started) then it just tells this object to 
			//destroy itself if this is not the original
			Destroy(this.gameObject);
            }
		}
	}

Thanks for the above code. Worked a treat.

is there other way to have only one "MUSIC" because the else condition(?) is not working on me

tha post helped me a lot !! thanks very much .

This is really helpful! Thanks :)

thank you, this was helpfull!

This is how ive done it for my main menu:

public class menuMusic : MonoBehaviour {

static bool AudioBegin = false; 

void Awake()
{
					if (!AudioBegin) {
					audio.Play ();
					DontDestroyOnLoad (gameObject);
					AudioBegin = true;
			} 
	}

void Update () {
	if(Application.loadedLevelName == "Upgraded")
	{
		audio.Stop();
		AudioBegin = false;
	}

}

}

and attach the audio to the same gameobject that contains this script.
just change the name of the level from “Upgraded” to whatever ones you want the
music to change on.

Thank you for your answer, it really helped me a lot ! But when i return to the first scene,i have two audiosources " it duplicate" ! can you help me ?

Is your audio file on "play on awake"? if so thats your problem

Thank you ! I've been struggling for few hours making a singleton and whatnot and finally come by this solution, perfectly simple!

Works nicely but is a bit of a battery killer on mobile devices (string compare 30 times a second for the entire duration of the app). Instead of the Update you can quite easily use public static methods to start and stop: public static void StartMusic() { audio.Play(); } public static void StopMusic() { audio.Stop(); }

Can you assist? I have my object set to a singleton, but when I attempt to stop on scene load it is saying that the source is not assigned. it is attached to the object, and it's not stopping the audio between scenes, but it's not letting me manipulate it.