Music on specific scenes?

So I am fairly new to unity and #C I have this don’t destroy script on a backgroundmusic object but I don’t want it to play in the menu after I return there from the game. Any idea about how to do this? Can I use the “Music” tag to check if there already is something there and then make it destroy itself?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class dontdestroy : MonoBehaviour {

    // Use this for initialization
    private void Awake()
    {
        GameObject[] objs = GameObject.FindGameObjectsWithTag("music");

        if (objs.Length > 1)
            Destroy(this.gameObject);
        DontDestroyOnLoad(this.gameObject);
    }
}

Use this: Unity - Scripting API: SceneManagement.SceneManager.sceneLoaded

Every time you load a scene, check if that scene is equal to your menu scene; if it is, stop the music.

1 Like

Hi, thanks for the response but it didn’t really help us out but our teacher showed up a few weeks later to help us out