- Home /
Keeping audio between all but one scene?
I want to keep my audio playing when I click on "Extras" and "Multiplayer" but I want it to stop when I click on "Singleplayer." Does anyone know how to do this? I have created an Audio Source and have made it a child to my Main Menu object. My Main Menu object contains the script where I click my GUI buttons to change scenes. So would I use "audio.Stop()" on my MainMenu script?
Here's my code for the MainMenu script:
#pragma strict
var myGUI : GUISkin;
function OnGUI()
{
GUI.skin = myGUI;
if(GUI.Button(Rect(Screen.width/2-100,Screen.height/2-80,200,50), "Single Player"))
{
Application.LoadLevel(1);
}
if(GUI.Button(Rect(Screen.width/2-100,Screen.height/2-20,200,50), "Multiplayer"))
{
Application.LoadLevel(2);
}
if(GUI.Button(Rect(Screen.width/2-100,Screen.height/2+40,200,50), "Extras"))
{
Application.LoadLevel(3);
}
if(GUI.Button(Rect(Screen.width/2-100,Screen.height/2+100,200,50), "Quit Game"))
{
Application.Quit();
}
}
Answer by Julien-Lynge · Jan 02, 2013 at 08:58 PM
To keep a GameObject (in this case the GO containing the audiosource) from being destroyed when loading a scene, use DontDestroyOnLoad (http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html).
To turn it off when entering a specific scene, use OnLevelWasLoaded (http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html), which is fired when a level is loaded. Check the name or number of the loaded level, and destroy or turn off the audio when you don't need it.
Your answer
Follow this Question
Related Questions
How to mute my volume 2 Answers
How to make in main menu? 5 Answers
play an audio clip from a GUI button 1 Answer
problems with continuous audio between scenes 1 Answer
Mute Unity Ads in code? 2 Answers