Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by smirlianos · Oct 18, 2012 at 12:31 PM · javascriptaudiomusicbegginerwav

How I can prevent music from stopping after changing a scene?

When I change scene, the music stops, how can i make it continue?

Comment
Add comment · Show 3
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 smirlianos · Oct 18, 2012 at 01:39 PM 0
Share

so, how???

avatar image Mrobertson · Oct 18, 2012 at 01:52 PM 1
Share

I'm not sure because I haven't done it but whatever gameobject you have playing the sound you would probably set as don't DestroyOnLoad.

avatar image smirlianos · Oct 18, 2012 at 02:24 PM 0
Share

how is the script like?

3 Replies

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

Answer by Noah-1 · Oct 18, 2012 at 02:35 PM

HI, as @Mrobertson said, you should use dontDestroyOnLoad in the object where your AudioSource is attached.Just add something like this in your script:

 function Awake () {
     DontDestroyOnLoad (transform.gameObject);
 }

Hope it helps.

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
avatar image
0

Answer by Argylelabcoat · Oct 18, 2012 at 06:18 PM

I personally like to use the async functions for loading scenes, they let you display loading graphics/loading sounds without hiccups:

  • Application.LoadLevelAsync

  • Application.LoadLevelAdditiveAsync

As Mrobertson stated, you can have your playback system to DontDestroyOnLoad, another option is to use additive scenes and keep pieces you want to delete as children beneath nicely named game objects so that you can say `GameObject.Destroy(GameObject.Find("Level2-Enemies"))` or something along said lines.

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
avatar image
0

Answer by AlucardJay · Oct 18, 2012 at 09:48 PM

When this question comes up, DontDestroyOnLoad is the answer given, but it is only half the solution. For example, if you load the scene where the music player is created, then a duplicate is made, now you have 2 players following you through all the scenes. It took me a while to find the solution, but the answer is to use a Singleton . Search singletons on this 'site (or in google search "unity singleton") as there would be better explanations than what I could give.

The simple version is : only one instance of a singleton exists at a time, two cannot exist. It can be called by all scripts by using a static variable. So only one, and it has a variable which other scripts can reference.

Here is the script for a working singleton, just attach it to the music player. Note : this is in unity JS, and the script must be called MusicSingleton :

 #pragma strict
 
 // change the class name here to the name of your script, e.g.
 // public class ThisIsTheScriptNameHere extends MonoBehaviour
  
 public class MusicSingleton extends MonoBehaviour
 {
     private static var instance : MusicSingleton;
     
     function Awake() 
     {
         if (instance != null && instance != this) 
         {
             Destroy( this.gameObject );
             return;
         } 
         else 
         {
             instance = this;
         }
         
         DontDestroyOnLoad( this.gameObject );
     }
     
     // also change this to your script name
     // public static function GetInstance() : ThisIsTheScriptNameHere
       
     public static function GetInstance() : MusicSingleton 
     {
         return instance;
     }
     
     function Update() 
     {
         //
     }
 }


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.

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

Why sound doesn't stop? 2 Answers

Playing Music Problem. 2 Answers

Make a command run only once 1 Answer

Ambisonic Audio working on PC but not Android 3 Answers

Loading music for initial scene loading takes a very long time 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