• 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
2
Question by gdebojyoti · Mar 08, 2013 at 05:12 PM · audio

Continuous music across multiple scenes

I want to incorporate a background music in my game menu. But when I return to the main menu (after viewing a submenu), another instance of the music starts, besides the one already playing - something which I obviously don't want. Please help.

I have already tried the following answers- none of them worked for me.

http://answers.unity3d.com/questions/200340/dont-start-the-audio-from-the-beginning.html http://answers.unity3d.com/questions/59135/playing-music-while-scenes-change.html http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html?sort=oldest http://answers.unity3d.com/questions/370424/dont-destroy-a-variable-when-changing-scene.html

Comment
Add comment · Show 6
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 AlucardJay · Mar 08, 2013 at 05:43 PM 2
Share

I thought the post you commented on that I replied to was fine (http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html?sort=oldest).

The problem you're probably having is implementing the Singleton (or just relabelling the typecast $$anonymous$$yUnitySingleton) . Here is a question I asked a while back with alot of information on Singletons : http://answers.unity3d.com/questions/235417/how-do-i-create-a-static-instance-in-javascript.html

But really you shouldn't have a problem with that other post.

 private static var instance : $$anonymous$$yUnitySingleton;

 public static function GetInstance() : $$anonymous$$yUnitySingleton {
     return instance;
 }
  
 function Awake() {
     if (instance != null && instance != this) {
         Destroy(this.gameObject);
         return;
     } else {
         instance = this;
     }
     DontDestroyOnLoad(this.gameObject);
 }

this script takes care of everything. The first time the scene is loaded, thereis no instance so the script assigns itself as the instance. Now when you go through , and then back to the first scene with that gameObject, Awake checks if (instance != null && instance != this), basically if there is something assigned to instance already, and the instance is not this gameObject, then Destroy this copy and leave the first one.

avatar image gdebojyoti · Mar 08, 2013 at 08:45 PM 0
Share

@alucardj: Actually, I hadn't noticed your answer before making this post. It did work perfectly. Thank you. :-)

avatar image AlucardJay · Mar 08, 2013 at 09:27 PM 0
Share

Great ! I have made my comment into an answer too then for future readers. Please accept so we both get some lovely karma =]

http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

avatar image AlucardJay · Mar 08, 2013 at 09:47 PM 0
Share

I would say yes, as this is not destroyed so it will be in each scene. If it is a music manager, just have your music management scripts and audio source attached.

avatar image gdebojyoti · Mar 08, 2013 at 10:08 PM 0
Share

Okay. Thank you. :-)

avatar image AlucardJay · Mar 08, 2013 at 10:12 PM 0
Share

I have to go for a few hours, but I think I have all your questions. When I get back I shall try to write an example for you regarding :

  • How to organize this gameObject

  • how to check what level is loaded

  • changing/turning music on and off depending on level

you don't want to destroy anything, just change the audio clip. Example co$$anonymous$$g soon.

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by AlucardJay · Mar 08, 2013 at 09:25 PM

Updated answer due to further questions :

Here is a unityJavaScript with all the things you want. It will play a different track for each level you set it to, or just tell it to stop playing for that scene. Make sure you have all your scenes in the Build Settings : http://docs.unity3d.com/Documentation/Manual/PublishingBuilds.html

Read the script, and cross-reference with the Unity Scripting Reference links I have provided.

You understand the singleton I guess. Then all the work is done in the function OnLevelWasLoaded : http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html

 #pragma strict
 
 public class MusicSingleton extends MonoBehaviour
 {
     public static var instance : MusicSingleton;
     
     public var musicMainMenu : AudioClip;
     public var musicLevel1 : AudioClip;
     
     function Awake() 
     {
         if ( instance != null && instance != this ) 
         {
             Destroy( this.gameObject );
             return;
         } 
         else 
         {
             instance = this;
         }
         
         DontDestroyOnLoad( this.gameObject );
     }
     
     function OnLevelWasLoaded( level : int )
     {
         if ( level == 0 )
         {
             audio.Stop();
             audio.clip = musicMainMenu;
             audio.Play();
         }
         else if ( level == 1 )
         {
             audio.Stop();
             audio.clip = musicLevel1;
             audio.Play();
         }
         else if ( level == 2 )
         {
             audio.Stop();
         }
     }
     
     public static function GetInstance() : MusicSingleton 
     {
         return instance;
     }
     
     function Update() 
     {
         //
     }
     
 }




Original answer :

I thought the post you commented on that I replied to was fine (http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html?sort=oldest).

The problem you're probably having is implementing the Singleton (or just re-labelling the typecast MyUnitySingleton) . Here is a question I asked a while back with alot of information on Singletons : http://answers.unity3d.com/questions/235417/how-do-i-create-a-static-instance-in-javascript.html

But really you shouldn't have a problem with that other post.

 private static var instance : MyUnitySingleton;

 public static function GetInstance() : MyUnitySingleton {
     return instance;
 }
  
 function Awake() {
     if (instance != null && instance != this) {
         Destroy(this.gameObject);
         return;
     } else {
         instance = this;
     }
     DontDestroyOnLoad(this.gameObject);
 }

this script takes care of everything. The first time the scene is loaded, thereis no instance so the script assigns itself as the instance. Now when you go through , and then back to the first scene with that gameObject, Awake checks if (instance != null && instance != this), basically if there is something assigned to instance already, and the instance is not this gameObject, then Destroy this copy and leave the first one.

Comment
Add comment · Show 5 · 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 gdebojyoti · Mar 08, 2013 at 09:36 PM 0
Share

What if I want to completely stop all music at a certain scene?

avatar image AlucardJay · Mar 08, 2013 at 09:40 PM 0
Share

You could just use the function OnLevelWasLoaded : http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnLevelWasLoaded.html

Or a less economical way would be to check what level is loaded in an Update or InvokeRepeating :

http://docs.unity3d.com/Documentation/ScriptReference/Application-loadedLevel.html

http://docs.unity3d.com/Documentation/ScriptReference/Application-loadedLevelName.html

avatar image AlucardJay · Mar 09, 2013 at 12:49 AM 0
Share

I have updated my answer to include the functionality you were asking about.

avatar image AlucardJay · Jun 25, 2013 at 01:10 PM 1
Share

If I was to do this answer now, I would use a switch-case ( every day I learn more ! ) :

 function OnLevelWasLoaded( level : int )
 {
     switch( level )
     {
         case 0 :
             audio.Stop();
             audio.clip = music$$anonymous$$ain$$anonymous$$enu;
             audio.Play();
         break;
         
         case 1 :
             audio.Stop();
             audio.clip = musicLevel1;
             audio.Play();
         break;
         
         case 2 :
             audio.Stop();
         break;
         
         default :
             audio.Stop();
         break;
     }
 }
avatar image Davevmb · Jul 31, 2014 at 11:31 AM 0
Share

For some reason, on my mac, the var function is not allowed. Could this have anything to do with mono ins$$anonymous$$d of .net ?

avatar image
2

Answer by zfz.zahid · Dec 08, 2014 at 11:17 AM

Create an empty game object in a scene and add the following code script to the gameobject using UnityEngine; using System.Collections; public class MyUnitySingleton : MonoBehaviour { private static MyUnitySingleton instance = null; public static MyUnitySingleton Instance { get { return instance; } } void Awake() { if (instance != null && instance != this) { Destroy(this.gameObject); return; } else { instance = this; } DontDestroyOnLoad(this.gameObject); } // any other methods you need } add audio source file to the gameobject.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 People are following this question.

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

Related Questions

Audio doesn't work in editor? 6 Answers

Using input.getbuttondown to play audio = NOT WORKING? :( 2 Answers

Song Allowments 1 Answer

How do I reduce the CPU load spike caused by playing an audio clip? 1 Answer

How can i choose what channel to play my audio by myself? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges