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 Frede1012 · Apr 28, 2012 at 10:02 PM · javascriptaudiomp3

Play music troughout whole game in js

Hello there. I have an .mp3 that plays when i start the first level, and i figured out to use dontdestroyonload to get it on to my next level. But it seems really far away. Is there a way to disable 3D sound so the music is not feeling far away. I fixed it in the first level by making the Object follow the Character, but there is no object now that i loaded the new level with dontdestroyonload. And when i die in level2 and retry the music is gone. Because i added an Destroy for the object when retrying. Because if i retry in level1 the music will then play twice if i dont destroy it?

Retry code:

 var showGUI : boolean = false ;


 function OnTriggerEnter(other : Collider){
 showGUI = true ;
 Time.timeScale = 0;
 AudioListener.volume = 0;

 }


 function OnGUI () {
 if(showGUI){

 GUI.Box (Rect ( 350, 160, 250, 200), "");
 var gameMusic : GameObject = GameObject.Find("GameMusic");

 if(GUI.Button (Rect ( 400, 200, 150, 30), "Retry Level" ))
 {

 Application.LoadLevel(Application.loadedLevel);
 Time.timeScale = 1;
 AudioListener.volume = 100;
 Destroy(gameMusic);
  
 }
  
 if(GUI.Button (Rect ( 400, 250, 150, 30), "Retry From Start" )) {
 Application.LoadLevel(1);
 AudioListener.volume = 100;
 Time.timeScale = 1;
 Destroy(gameMusic);
 
 }
  
 if(GUI.Button (Rect ( 400, 300, 150, 30), "Main Menu" )) {
 Application.LoadLevel(0);
 AudioListener.volume = 100;
 Time.timeScale = 1;
  

 }
 }
 }

So basicly i am trying to get the music to play in all levels except mainmenu, and not sounding far away.

EDIT: Okay so it works now in level2 with retry. I fixed it by creating 2 scripts, one for level1 and one for other levels. But how do i stop it from "spawning" and "playing" the same Audio file ontop of the first one when reloading level1? Without starting the music over.

  • Frederik

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
2
Best Answer

Answer by BiG · Apr 28, 2012 at 10:11 PM

Just click to your .mp3 file, look through the inspector, and uncheck the "3D Sound" box! You will listen to the music "far away" no more.

And yes, you have to attach the .mp3 file to an object that's never deleted, and that will persist between scenes change. The Dontdestroyonload function is the correct trick.

Comment
Add comment · Show 6 · 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 Frede1012 · Apr 28, 2012 at 10:19 PM 0
Share

But when i Retry the first level that has the "real" object. It plays the music twice..

avatar image BiG · Apr 28, 2012 at 10:29 PM 1
Share

Look at your code:

 if(GUI.Button (Rect ( 400, 250, 150, 30), "Retry From Start" )) {
 Application.LoadLevel(1);
 AudioListener.volume = 100;
 Time.timeScale = 1;
 Destroy(game$$anonymous$$usic);

You have an Application.LoadLevel FOLLOWED by other instructions. These instructions won't be executed, because the new level has already been loaded!

In other words, the Application.LoadLevel has to be called as the FINAL instruction of a branch of your code. Like this:

 if(GUI.Button (Rect ( 400, 250, 150, 30), "Retry From Start" )) {
 AudioListener.volume = 100;
 Time.timeScale = 1;
 Destroy(game$$anonymous$$usic);
 Application.LoadLevel(1);

Correct all the errors like this! Attention: by the way, this will restart your audio track, and this is not very related to mantain the sound with dontdestroyonload...

avatar image Frede1012 · Apr 28, 2012 at 10:35 PM 0
Share

It has nothing to do with that. That was just a tempoary fix for the playing twice "bug". I am trying to make the music play troughout all levels, also if i retry the level, but the problem is that when i retry level1 it plays twice, thats why i created the fix that destroyed it on retry. But then it doesnt work in level2 because then it destroys on retry there too.. And it doesnt have to in level2. Because the "real" AudioSource is not in level2 but in level1.

avatar image Frede1012 · Apr 28, 2012 at 10:43 PM 0
Share

Okay so it works now in level2 with retry. I fixed it by creating 2 scripts, one for level1 and one for other levels. But how do i stop it from "spawning" and "playing" the same Audio file ontop of the first one when reloading level1? Without starting the music over.

avatar image BiG · Apr 30, 2012 at 06:40 PM 0
Share

It's strange. If you've created ONE object with the audio file attached to it, assigning a script with a dontdestroyonload function, you shouldn't have the problem of a "double" playing.

$$anonymous$$aybe your game$$anonymous$$usic is a prefab, and you are instantiating it every time you load the scene? It's the only explanation.

If I'm wrong, sorry, but I can't see other reasons for that, so I think that I couldn't help you more than this.

If other users read this, feel free to join the topic.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't get a sound to loop 2 Answers

Converting .wav to .mp3 0 Answers

Audio, mp3 lisence question 0 Answers

How to remove a word from string 0 Answers

Help Needed! expecting (, found 'Chop'. 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