Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 FesterTY · Mar 02, 2019 at 12:43 PM · scripting problemaudioscene-loading

I want music to continuously play even when the scene restarts

Hey guys, so I want the MusicManager game object to continuously play with new scenes loading in. The code below works, but does not archive what I wanted. There's a game over state. And when the player is in that state, the scene restarts.

Let me firstly explain what's going on inside the code: MusicManager detects another music manager, it destroys the one which is current inside "DontDestroyOnLoad" stage.

The code would work, but for some scenes, I added my own MusicManager to change the music playing in that scene, and because of this, whenever the scene restarts, the music also restarts..

 public class MusicManager : MonoBehaviour
 {
     static MusicManager instance;
 
     private void Awake()
     {
         CheckInstance();
     }
 
     void CheckInstance()
     {
         // This gets check with every MusicManager
         // UNLIKE LOCALS, THE GLOBAL INSTANCE WILL ONLY CHECK ONCE ON AWAKE
 
         if (instance == null)
         {
             instance = this;
             DontDestroyOnLoad(instance.gameObject);
         }
         else
         {
             Destroy(instance.gameObject);
         }
     }
 }

Any advice are appreciated! Thanks in advance.

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
1

Answer by Okido · Mar 02, 2019 at 01:37 PM

I think it's because you're checking if there is already a music manager, and then if there is, you're destroying it - but that defeats the point of DontDestroyOnLoad :p - because you are destroying it on load.

You want to check if there is already a music manager, and if there is, do nothing. If not, only then should you create a new one.

When you're destroying the old one and creating a new one, the music is restarting because it's a newly created manager in that scene

EDIT: Added code below - in your else statement, you should be destroying the gameobject, rather than the instance. Destroying the instance was destroying the one you had preserved, rather than the new object that had just awoken.

 if (instance == null)
         {
             instance = this;
             DontDestroyOnLoad(instance.gameObject);
         }
         else
         {
             Destroy(gameObject);
         }



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 FesterTY · Mar 02, 2019 at 01:51 PM 0
Share

Hi, Okido! Firstly, thank you for the advice. I really do appreciate it.

Secondly, if I do nothing when there is already a music manager then whenever the player dies, and the level restarts, then the music will also restart. Let's say in my level 1 scene, I have a music manager to play "cal$$anonymous$$g" music, but without "DontDestroyOnLoad", the music will get restart every time the player dies.

When you're destroying the old one and creating anew one

The problem is that I want the music manager to be inside DontDestroyOnLoad(), because when the game restarts, the music will continue regardless. That isn't what's happening. What's happening is that the music manager doesn't get put into DontDestroyOnLoad().. Thanks for the advice, regardless!
avatar image Okido FesterTY · Mar 02, 2019 at 03:16 PM 1
Share

No worries! I tried your code out and I assume the object you want to preserve is in the scene you're reloading. So every time you reloaded the scene, the reason the object wasn't going into DontDestroyOnLoad was because you were destroying it (because you had the DDoL object set as the instance, and in the code you have you're destroying the instance if it exists).

Edit: I figured out the reason and updated this comment/ my answer so it's more visible (:

 public class $$anonymous$$usic$$anonymous$$gr : $$anonymous$$onoBehaviour
 {
     static $$anonymous$$usic$$anonymous$$gr instance;
 
     void Awake()
     {
         DontDestroyOnLoad(this);
 
         if (instance == null)
         {
             instance = this;
         }
         else
         {
             Destroy(gameObject);
         }
     }
avatar image FesterTY Okido · Mar 03, 2019 at 05:56 AM 0
Share

Alright, I'm really sorry for the late response! But thank you for helping me out! Your solution does work, and put the object inside DDoL. Problem is, it destroys the $$anonymous$$usic$$anonymous$$anager that is already inside the scene. The music does persist and continuously play, but it's not the music that is in the scene, it's the music from the previous scene.

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

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

Related Questions

Audio Mixer exposed parameter path error? 0 Answers

How to fix a Missing Refrence Exception Error 1 Answer

How to loop through a list of sounds if(bool) 1 Answer

Why the audio sound effects of the main menu are not hearing like in mute ? 0 Answers

Unity keep crashing when loading scene from a couritne (Unity 5.x) 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