Play music troughout whole game in js

Hello there.
I have an .mp3 that plays when i start the first level, and i figured out to use dontdestroyonload to get it on to my next level. But it seems really far away. Is there a way to disable 3D sound so the music is not feeling far away.
I fixed it in the first level by making the Object follow the Character, but there is no object now that i loaded the new level with dontdestroyonload.
And when i die in level2 and retry the music is gone. Because i added an Destroy for the object when retrying. Because if i retry in level1 the music will then play twice if i dont destroy it?

Retry code:

var showGUI : boolean = false ;

function OnTriggerEnter(other : Collider){
showGUI = true ;
Time.timeScale = 0;
AudioListener.volume = 0;

}

function OnGUI () {
if(showGUI){

GUI.Box (Rect ( 350, 160, 250, 200), "");
var gameMusic : GameObject = GameObject.Find("GameMusic");

if(GUI.Button (Rect ( 400, 200, 150, 30), "Retry Level" ))
{

Application.LoadLevel(Application.loadedLevel);
Time.timeScale = 1;
AudioListener.volume = 100;
Destroy(gameMusic);

}
 
if(GUI.Button (Rect ( 400, 250, 150, 30), "Retry From Start" )) {
Application.LoadLevel(1);
AudioListener.volume = 100;
Time.timeScale = 1;
Destroy(gameMusic);

}
 
if(GUI.Button (Rect ( 400, 300, 150, 30), "Main Menu" )) {
Application.LoadLevel(0);
AudioListener.volume = 100;
Time.timeScale = 1;
 

}
}
}

So basicly i am trying to get the music to play in all levels except mainmenu, and not sounding far away.

EDIT:
Okay so it works now in level2 with retry. I fixed it by creating 2 scripts, one for level1 and one for other levels. But how do i stop it from “spawning” and “playing” the same Audio file ontop of the first one when reloading level1? Without starting the music over.

  • Frederik

Just click to your .mp3 file, look through the inspector, and uncheck the “3D Sound” box! You will listen to the music “far away” no more.

And yes, you have to attach the .mp3 file to an object that’s never deleted, and that will persist between scenes change. The Dontdestroyonload function is the correct trick.