- Home /
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
Answer by BiG · Apr 28, 2012 at 10:11 PM
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.
But when i Retry the first level that has the "real" object. It plays the music twice..
Look at your code:
if(GUI.Button (Rect ( 400, 250, 150, 30), "Retry From Start" )) {
Application.LoadLevel(1);
AudioListener.volume = 100;
Time.timeScale = 1;
Destroy(game$$anonymous$$usic);
You have an Application.LoadLevel FOLLOWED by other instructions. These instructions won't be executed, because the new level has already been loaded!
In other words, the Application.LoadLevel has to be called as the FINAL instruction of a branch of your code. Like this:
if(GUI.Button (Rect ( 400, 250, 150, 30), "Retry From Start" )) {
AudioListener.volume = 100;
Time.timeScale = 1;
Destroy(game$$anonymous$$usic);
Application.LoadLevel(1);
Correct all the errors like this! Attention: by the way, this will restart your audio track, and this is not very related to mantain the sound with dontdestroyonload...
It has nothing to do with that. That was just a tempoary fix for the playing twice "bug". I am trying to make the music play troughout all levels, also if i retry the level, but the problem is that when i retry level1 it plays twice, thats why i created the fix that destroyed it on retry. But then it doesnt work in level2 because then it destroys on retry there too.. And it doesnt have to in level2. Because the "real" AudioSource is not in level2 but in level1.
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.
It's strange. If you've created ONE object with the audio file attached to it, assigning a script with a dontdestroyonload function, you shouldn't have the problem of a "double" playing.
$$anonymous$$aybe your game$$anonymous$$usic is a prefab, and you are instantiating it every time you load the scene? It's the only explanation.
If I'm wrong, sorry, but I can't see other reasons for that, so I think that I couldn't help you more than this.
If other users read this, feel free to join the topic.
Your answer
Follow this Question
Related Questions
Can't get a sound to loop 2 Answers
Converting .wav to .mp3 0 Answers
Audio, mp3 lisence question 0 Answers
How to remove a word from string 0 Answers