[SOLVED] Playing audio depending on scene

I’m fairly new to both Unity and C#, so apologies in advance if this is a dumb question.

I’m trying to create a game in which a specific song plays depending on the scene the player is in.

I’ve written code (see below) for this, which has no errors, and attached it to an empty GameObject called MusicManager (also see below), and it works fine, - for a time. It will play the correct music for the level I start the game is (so it’ll play the menu song if I start in MainMenu, and it’ll play the Level1 song if I start in Level01), but if I change scenes, the song keeps playing and a new one doesn’t start.

I’ve looking at this from every angle I can think of for a while, so help will be much appreciated. Thanks in advance for any answers.

MusicManager.cs
using UnityEngine;
using System.Collections;

public class MusicManager : MonoBehaviour {

	static MusicManager instance = null;
	
	public AudioClip[] soundtrack;

	// Use this for initialization
	void Awake () {
		Debug.Log(Application.loadedLevelName);
		
		soundtrack[0] = (AudioClip)Resources.Load("Songs/Level1");
		soundtrack[1] = (AudioClip)Resources.Load("Songs/Level2");
		soundtrack[2] = (AudioClip)Resources.Load("Songs/Level3");
		soundtrack[3] = (AudioClip)Resources.Load("Songs/Level4");
		soundtrack[4] = (AudioClip)Resources.Load("Songs/Level5");
		soundtrack[5] = (AudioClip)Resources.Load("Songs/Level6");
		soundtrack[6] = (AudioClip)Resources.Load("Songs/Level7");
		soundtrack[7] = (AudioClip)Resources.Load("Songs/Level8");
		soundtrack[8] = (AudioClip)Resources.Load("Songs/Level9");
		soundtrack[9] = (AudioClip)Resources.Load("Songs/Level10");
		soundtrack[10] = (AudioClip)Resources.Load("Songs/BeatGame");
		soundtrack[11] = (AudioClip)Resources.Load("Songs/Menus");
		
		if (Application.loadedLevelName == "MainMenu") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "Credits01") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "Credits02") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "Credits03") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LevelSelect") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS01") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS02") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS03") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS04") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS05") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS06") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS07") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS08") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS09") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "LS10") {
			audio.clip = soundtrack[11];
			audio.Play();
		} else if (Application.loadedLevelName == "Win") {
			audio.clip = soundtrack[10];
			audio.Play();
		} else if (Application.loadedLevelName == "Level01") {
			audio.clip = soundtrack[0];
			audio.Play();
		} else if (Application.loadedLevelName == "Level02") {
			audio.clip = soundtrack[1];
			audio.Play();
		} else if (Application.loadedLevelName == "Level03") {
			audio.clip = soundtrack[2];
			audio.Play();
		} else if (Application.loadedLevelName == "Level04") {
			audio.clip = soundtrack[3];
			audio.Play();
		} else if (Application.loadedLevelName == "Level05") {
			audio.clip = soundtrack[4];
			audio.Play();
		} else if (Application.loadedLevelName == "Level06") {
			audio.clip = soundtrack[5];
			audio.Play();
		} else if (Application.loadedLevelName == "Level07") {
			audio.clip = soundtrack[6];
			audio.Play();
		} else if (Application.loadedLevelName == "Level08") {
			audio.clip = soundtrack[7];
			audio.Play();
		} else if (Application.loadedLevelName == "Level09") {
			audio.clip = soundtrack[8];
			audio.Play();
		} else if (Application.loadedLevelName == "Level10") {
			audio.clip = soundtrack[9];
			audio.Play();
		}
			
		if (instance != null) {
			Destroy(gameObject);
			Debug.Log ("Duplicate destroyed.");
		} else {
			instance = this;
			GameObject.DontDestroyOnLoad(gameObject);
		}
	}
}

MusicManager object

MusicManager object when game is being played

Hi

1 . All objects in the scene will be destroyed when loading a new level. Therefore, you should use “DontDestroyOnLoad” to avoid your script being destroyed when switching the scenes.

  void Awake() {
            DontDestroyOnLoad(transform.gameObject);
   }

http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

2 . Awake () is only called once at the beginning of loading the script. Therefore, it can’t check your condition (i.e. Application.loadedLevelName) continuously.

:]