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:
var myGUI : GUISkin;
GUI.skin = myGUI;
if(GUI.Button(Rect(Screen.width/2-100,Screen.height/2-80,200,50), "Single Player"))
if(GUI.Button(Rect(Screen.width/2-100,Screen.height/2+100,200,50), "Quit Game"))
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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
9 People are following this question.
How to mute my volume
Audio Stutters on Scene Transition
How can I dynamically create scene ?
Programming a texture to act as a button...
play an audio clip from a GUI button