- Home /
Audio continuation through two scenes
Hey, I have two menu scenes, one called "Menu" and another called "instructionsMenu". Just wondering if there is any way to carry on playing an audio source throughout both of these scenes without it cutting out? I've read this: http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html
However, i'm looking to learn more about JavaScript so id like to carry on using the same language throughout my game (that and i can't read C# as well)
Thanks in advance!
Use this link ins$$anonymous$$d: http://answers.unity3d.com/questions/11314/ as currently pasted it leads to a "Page Not Found" because the link doesn't include the last "l" of .html
Answer by TheCodeMonkey · Dec 06, 2011 at 04:21 PM
The solution in that page was not exactly C# specific, all he really said was to use:
DontDestroyOnLoad(this.gameObject);
to keep the sound playing through from the first scene. Then he employed a strategy of making sure that there is only ever one in every scene, you can do something like this, or simply deciding to only ever create one.
http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html
EDIT: actually if I am not mistaken he even has a javascript example directly below his C# example, and he does have a very nice and in depth answer
http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html
just gonna link to his
Ah, i thought that DontDestroyOnLoad was C# specific. I'm still relatively new to program$$anonymous$$g And you are right! Clearly i didn't read the answer properly, i just saw the first one and assumed it was all C#. I'll look into it a bit more, thanks for letting me know
I managed to get the sound to carry on playing through both scenes, but when i click on the "back" button to return to the "$$anonymous$$enu" scene, it created another instance of the music object. I think they describe how to fix this in the link, but i don't understand it very well. Any help? I currently have:
var button1 : Texture;
var button2 : Texture;
var skin : GUISkin;
var music : GameObject;
var musicOn = false;
function OnGUI () {
GUI.skin=skin;
Screen.showCursor = true;
Screen.lockCursor = false;
if(GUI.Button (Rect(1000, 384, 200, 100), button1))
{
Application.LoadLevel("GTAR1");
}
if(GUI.Button (Rect(1000, 500, 200, 100), button2))
{
Application.LoadLevel("instructions$$anonymous$$enu");
DontDestroyOnLoad(music);
}
}
Answer by MrErk · Mar 08, 2015 at 04:46 PM
If anyone where to come here looking for a simple way of solving this, I created this and it works perfectly for me!
static bool AudioBegin = false;
GameObject otherSound;
void Awake()
{
otherSound = GameObject.FindGameObjectWithTag("Game Music");
if (otherSound == this.gameObject)
{
if (!AudioBegin)
{
DontDestroyOnLoad(this.gameObject);
AudioBegin = true;
}
}
else
{
Destroy(this.gameObject);
}
}
Add this code to a GameObject that has an AudioSource with an attached music file, and give it the tag "Game Music".
Cheers!
I can't seem to get this code working for me. The music stops playing when I progress to the next level. Any suggestions
$$anonymous$$rErk your code just solved my problems, it was very easy to implement, thanks man!!
Answer by Lukey_BlueNose · Nov 05, 2016 at 09:21 PM
hi all... I'm having a similar problem... the GameObject i have attached the Audio Source to isn't being destroyed but it makes no sound when the new scene loads... check sound settings, position, 3d sound, volume etc... is there anything else that needs to be done in the script to a game object with audio apart from not destroying it for this to work? Please and thank you @TheCodeMonkey @MrErk
Your answer
Follow this Question
Related Questions
How can I check the scene and play audio accordingly? 1 Answer
How do I play audio once in Update? 1 Answer
How to play music/audio 2 Answers
Audio clip not playing 1 Answer
Audio not working when loading scene for second time 0 Answers