Continuous music across multiple scenes

I want to incorporate a background music in my game menu.
But when I return to the main menu (after viewing a submenu), another instance of the music starts, besides the one already playing - something which I obviously don’t want.
Please help.

I have already tried the following answers- none of them worked for me.

http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html?sort=oldest

Updated answer due to further questions :

Here is a unityJavaScript with all the things you want. It will play a different track for each level you set it to, or just tell it to stop playing for that scene. Make sure you have all your scenes in the Build Settings : Unity - Manual: Publishing Builds

Read the script, and cross-reference with the Unity Scripting Reference links I have provided.

You understand the singleton I guess. Then all the work is done in the function OnLevelWasLoaded : http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html

#pragma strict

public class MusicSingleton extends MonoBehaviour
{
	public static var instance : MusicSingleton;
	
	public var musicMainMenu : AudioClip;
	public var musicLevel1 : AudioClip;
	
	function Awake() 
	{
		if ( instance != null && instance != this ) 
		{
			Destroy( this.gameObject );
			return;
		} 
		else 
		{
			instance = this;
		}
		
		DontDestroyOnLoad( this.gameObject );
	}
	
	function OnLevelWasLoaded( level : int )
	{
		if ( level == 0 )
		{
			audio.Stop();
			audio.clip = musicMainMenu;
			audio.Play();
		}
		else if ( level == 1 )
		{
			audio.Stop();
			audio.clip = musicLevel1;
			audio.Play();
		}
		else if ( level == 2 )
		{
			audio.Stop();
		}
	}
	
	public static function GetInstance() : MusicSingleton 
	{
		return instance;
	}
	
	function Update() 
	{
		//
	}
	
}

Original answer :

I thought the post you commented on that I replied to was fine (http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html?sort=oldest).

The problem you’re probably having is implementing the Singleton (or just re-labelling the typecast MyUnitySingleton) . Here is a question I asked a while back with alot of information on Singletons : how do I create a static Instance in javascript - Questions & Answers - Unity Discussions

But really you shouldn’t have a problem with that other post.

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 script takes care of everything. The first time the scene is loaded, thereis no instance so the script assigns itself as the instance. Now when you go through , and then back to the first scene with that gameObject, Awake checks if (instance != null && instance != this), basically if there is something assigned to instance already, and the instance is not this gameObject, then Destroy this copy and leave the first one.

Create an empty game object in a scene and add the following code script to the gameobject
`using UnityEngine;
using System.Collections;

public class MyUnitySingleton : MonoBehaviour {

private static MyUnitySingleton instance = null;
public static MyUnitySingleton Instance {
	get { return instance; }
}
void Awake() {
	if (instance != null && instance != this) {
		Destroy(this.gameObject);
		return;
	} else {
		instance = this;
	}
	DontDestroyOnLoad(this.gameObject);
}

// any other methods you need

}
`
add audio source file to the gameobject.