Change music between scenes

I have two music clips, one for the menu and one for the in game level, I would like those music clips to be played on specific scenes, so when the game starts and the menu is displayed, I want one song to play and then when you change to a level scene, it turns off the menu music and turns on the level music and then, when you change from the level scene to the menu scene, the music changes again. I have made a script, for this but the problem is that it works in the unity editor, but when I build the game for iPhone the music won’t start when you change from level scene to menu scene.

My script:

static var TurnOffMenuMusic : boolean = false;

static var pauseUnPauseBut : boolean = false;

var menuMusic : AudioSource;

function Start () {

if (TurnOffMenuMusic == false) {
		audio.Play();
	} else if  (TurnOffMenuMusic == true) {
		audio.Pause();
	}
}


function Update () {

	if (TurnOffMenuMusic == false) {
		DontDestroyOnLoad(transform.gameObject);
	} else if  (TurnOffMenuMusic == true) {
		Destroy(this.gameObject);
	}
	
}

And then to activate the boolean I have this script.

var targetScript : MenuMusicOnandOff;

function Awake() {

targetScript.TurnOffMenuMusic = true;

}

And then I have a singleton to make sure the music object won’t duplicate.

private static var instance:MyUnitySingleton;

public static function GetInstance(): MyUnitySingleton {
return instance;
}

function Awake() {
	
	if (instance != null && instance != this) {
		Destroy(this.gameObject);
		return;
	} else {
		instance = this;
	}
	DontDestroyOnLoad(this.gameObject);
}

This is a possible solution to the original problem following on Fornoreason1000’s suggestion in the original post comments. You can use a music player script designed along these lines:

using UnityEngine;
using System.Collections;

/// <summary>
/// This script is in charge of playing music in the game
/// </summary>
public class MusicPlayer : MonoBehaviour
{
	/// <summary>
	/// The clip to play in a menu.
	/// This field is private because it's not designed to be directly
	/// modified by other scripts, and tagged with [SerializeField] so that
	/// you can still modify it using the Inspector and so that Unity
	/// saves its value.
	/// </summary>
	[SerializeField]
	private AudioClip menuMusic;
	
	/// <summary>
	/// The clip to play outside menus.
	/// </summary>
	[SerializeField]
	private AudioClip levelMusic;

	[SerializeField]
	/// <summary>
	/// The component that plays the music
	/// </summary>
	private AudioSource source;

	/// <summary>
	/// This class follows the singleton pattern and this is its instance
	/// </summary>
	static private MusicPlayer instance;

	/// <summary>
	/// Awake is not public because other scripts have no reason to call it directly,
	/// only the Unity runtime does (and it can call protected and private methods).
	/// It is protected virtual so that possible subclasses may perform more specific
	/// tasks in their own Awake and still call this base method (It's like constructors
	/// in object-oriented languages but compatible with Unity's component-based stuff.
	/// </summary>
	protected virtual void Awake() {
		// Singleton enforcement
		if (instance == null) {
			// Register as singleton if first
			instance = this;
			DontDestroyOnLoad(this);
		} else {
			// Self-destruct if another instance exists
			Destroy(this);
			return;
		}
	}

	protected virtual void Start() {
		// If the game starts in a menu scene, play the appropriate music
		PlayMenuMusic();
	}
	
	/// <summary>
	/// Plays the music designed for the menus
	/// This method is static so that it can be called from anywhere in the code.
	/// </summary>
	static public void PlayMenuMusic ()
	{
		if (instance != null) {
			if (instance.source != null) {
				instance.source.Stop();
				instance.source.clip = instance.menuMusic;
				instance.source.Play();
			}
		} else {
			Debug.LogError("Unavailable MusicPlayer component");
		}
	}
	
	/// <summary>
	/// Plays the music designed for outside menus
	/// This method is static so that it can be called from anywhere in the code.
	/// </summary>
	static public void PlayGameMusic ()
	{
		if (instance != null) {
			if (instance.source != null) {
				instance.source.Stop();
				instance.source.clip = instance.levelMusic;
				instance.source.Play();
			}
		} else {
			Debug.LogError("Unavailable MusicPlayer component");
		}
	}
}

Then wherever you need to change music in a script (in a button click callback, an Awake method or whatever), instead of setting a boolean value that will be checked in Update, you simply put

MusicPlayer.PlayMenuMusic();

or

MusicPlayer.PlayLevelMusic();

I use a similar architecture in my current project. To change the music, we have another script called GameControle that stays alive between scenes (using DontDestroyOnLoad). In its OnLevelWasLoaded callback it calls the music player to change its current clip (and it does also other stuff that needs to be done when the level changes). It’s better in my opinion than having a script for each scene in charge of changing the music, because everything related to music changes on level loading is kept in one place and therefore easier to maintain.