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);
    }
}

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

@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?

i am getting an error while trying the code above, about nullreference expected at the cases when it should do source.clip = Musicclips[0];.