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 Daro1155 · Jan 05, 2013 at 05:11 PM · timemusiclevelscontinue

Music continued in the all level

Hello, how i can make that music, which will play in the whole time, in first and second level, or more .

Comment
Add comment
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

1 Reply

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

Answer by AlucardJay · Jan 06, 2013 at 06:40 AM

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() 
     {
         //
     }
 }



from my answer here : http://answers.unity3d.com/questions/334376/how-i-can-prevent-music-from-stopping-after-changi.html

This is the question I originally asked when I started : http://answers.unity3d.com/questions/235417/how-do-i-create-a-static-instance-in-javascript.html

Comment
Add comment · Show 3 · 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 AlucardJay · Jan 15, 2013 at 03:55 AM 0
Share

How did you go?

avatar image Fattie · Mar 17, 2013 at 10:06 AM 1
Share

Just FTR @Alu

"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"

singletons are critical indeed and one must know everything about them.

(they are used everywhere in iOS for example, if you're an xcode user. Singletons are central to everything Apple has done with Cocoa, XCode, iOS...pretty much everything important is a singleton in iOS! -- the app delegate, every service like the accelerometer etc etc.)

anyway I was just going to say, in this particular example, background music. I probably wouldn't use a singleton.

in every game (other than a trivial test project), you have a "special setup scene" that loads first because you inevitably have heaps of things (including singletons!) maybe databases, tracking, whatever, that you inevitably need to start up before you launch the first "consumer scene".

(what i mean is, the special "start up scene" very likely wouldn't even have any visuals at all, perhaps just carry through the "loading screen" if anything)

(in partcular you inevitably want to launch "something, anything" that happens while you then load the enormous first scene)

in that "startup scene" you'd just have your background music, I'd say, and of course mark it to last forever.

(this is so ubiquitous it's surprising that unity don't just build in the concept.)

i find also inevitably, with the BG music, you'll have some sort of thing that has all the sound effects for the game (perhaps the UI sfx, or the game sfx, or both) Similarly that thing sticks around the whole game.

again just a thought .... in the case of BG music specifically, it is perfectly suited to loading at the beginning and just marking it to remain in place (perhaps load it with your "special non-civillian startup scene" which is inevitable in any game)

avatar image AlucardJay · Mar 17, 2013 at 10:14 AM 1
Share

As always, some excellent insight, plenty of information for me to absorb and learn from. I am very grateful for you advice, many thanks.

(all your answers seem accepted these days, so all I can do is vote your informative answers. We are lucky to have you active here on UA!)

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

10 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

Related Questions

Stop continuing music (Please Help) 1 Answer

Unmuted sounds playing off-beat 0 Answers

Loading A Level After 40 Seconds 2 Answers

Audio loop 3 Answers

time between scenes (loading time) 0 Answers


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