- Home /
How to have different background music through different scenes?
I have a lot of 2D scenes, and I want Scene 1 - 3 to have the same background music playing seamlessly if the user changes between them. However, I also want to change this background music so that when the user enters Scene 4 - 6, it changes to another track but also plays seamlessly between scenes 4 - 6.
I'm currently using this code to play the sound between scenes, but because i'm fairly new to unity & c#, I can't find a solution to stop this audio and get it to start playing another when Scene 4 starts.
using UnityEngine;
public class DoNotDestroy : MonoBehaviour
{
private void Awake()
{
GameObject[] musicObj = GameObject.FindGameObjectsWithTag("GameMusic");
if( musicObj.Length > 1)
{
Destroy(this.gameObject);
}
DontDestroyOnLoad(this.gameObject);
}
}
Answer by Llama_w_2Ls · Apr 28, 2021 at 07:09 AM
When you assign an audio clip to an audio source, it plays that new clip from the start. You can use the SceneManager events to check what music to assign to your audio source when a new scene is loaded, depending on the scene name. Here's a quick example:
public class BackgroundMusic : MonoBehaviour
{
static BackgroundMusic instance;
// Drag in the .mp3 files here, in the editor
public AudioClip[] MusicClips;
public AudioSource Audio;
// Singelton to keep instance alive through all scenes
void Awake()
{
if (instance == null) { instance = this; }
else { Destroy(gameObject); }
DontDestroyOnLoad(gameObject);
// Hooks up the 'OnSceneLoaded' method to the sceneLoaded event
SceneManager.sceneLoaded += OnSceneLoaded;
}
// Called whenever a scene is loaded
void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
{
// Replacement variable (doesn't change the original audio source)
AudioSource source = new AudioSource();
// Plays different music in different scenes
switch (scene.name)
{
case "Scene1":
source.clip = MusicClips[0];
break;
case "Scene2":
source.clip = MusicClips[1];
break;
default:
source.clip = MusicClips[2];
break;
}
// Only switch the music if it changed
if (source.clip != Audio.clip)
{
Audio.enabled = false;
Audio.clip = source.clip;
Audio.enabled = true;
}
}
}
If the scene's name is Scene1 it will play clip 0, Scene2 will play clip 1, and any other scene will play clip 2. Hope that helps. @karina_bandres
Thank you so much! It does work because I see the audio source changing whenever I change scenes; but I cant seem to hear it? I can hear other things like the click sound that I have when I click/hover on a button, it's just the music that I can't. I changed the code to match my scenes names and added the audio on the editor, and even when I click play the audio clip on audio source does change according to the code and the scene, which makes me wonder why it isn't playing any music? I don't have the mute audio feature on either. Any ideas on what i'm doing wrong? I added a screencap of the editor to see if it helps somehow.
It probably needs to start again. If you disable, then enable the audio source component, the music should play again hopefully. @karina_bandres
That actually worked! However I have to disable it and then enable it again on every change of scene / every time I click play; is it supposed to do that? I tried restarting unity and removing the code and audio source then adding it again and it still needs me to disable/enable it.
@Llama_w_2Ls I'm looking for the same thing. I tried your suggestion but I get 2 errorw saying: "The type or namespace name 'Scene' could not be found. The same error for 'LoadSceneMode'.
What can I do?
You probably want to include the namespace at the top of your file. Make sure you add using UnityEngine.SceneManagement
to the top of your file @fonchifox
Answer by Vugtjongen · 5 days ago
i am getting an error while trying the code above, about nullreference expected at the cases when it should do source.clip = Musicclips[0];.
sorry for sending so many mesagges i fixed the problems so i dont need help anymore.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to stop audio in a scene when DontDestroyOnLoad was already called? 1 Answer
Play again music problem. (Please help) :) 0 Answers
Multiple Cars not working 1 Answer
How to change animation's speed in C#? 2 Answers