• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ImpactUK · Dec 06, 2011 at 03:13 PM · audiosceneaudiosourceplayaudio source

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ForceMagic · Mar 24, 2015 at 06:32 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ImpactUK · Dec 06, 2011 at 04:29 PM 1
Share

Ah, i thought that DontDestroyOnLoad was C# specific. I'm still relatively new to programming 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

avatar image ImpactUK · Dec 06, 2011 at 06:36 PM 0
Share

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);

     }

}

avatar image
1

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image haynick888 · Apr 30, 2016 at 11:36 AM 0
Share

I can't seem to get this code working for me. The music stops playing when I progress to the next level. Any suggestions

avatar image heo598 · Sep 23, 2016 at 02:42 PM 0
Share

$$anonymous$$rErk your code just solved my problems, it was very easy to implement, thanks man!!

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

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.



Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Timeline Audio Source problem using Cinemachine 1 Answer

Active audiosource change on button press. Making a ingame radio. 1 Answer

AudioSource.clip.time won't work? 2 Answers

How to control backsound on/off in the third scene 1 Answer

AudioSource.PlayOneShot playing loud and garbled 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges