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
1
Question by CoalCzar · Jan 15, 2014 at 07:47 PM · musicdontdestroyonloadsingleton

Music Manager script. Do I want a singleton?

I have a script attached to an object with to play music in my game. The only problem I have is that the level in which the object spawns (level1) is returned to sometimes within the game and then there are two music objects playing at the same time.

I've read up a bit on Singletons, but I'm unsure if it would benefit me or how I would implement it in this instance. The script is below:

 using UnityEngine;
 using System.Collections;
 
 public class MusicScript : MonoBehaviour {
 
     private AudioSource mySource;
 
     void Awake()
     {
         DontDestroyOnLoad(gameObject);
         if (Application.loadedLevelName == "Start")
         {
             Destroy(gameObject);
         }
     }
 
     // Use this for initialization
     void Start () 
     {
         mySource = GetComponent<AudioSource>();
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (MenuController.gamePaused)
         {
             mySource.Pause();
         }
         else if (!mySource.isPlaying)
         {
             mySource.Play();
         }
     }
 }

Thanks.

Comment
Add comment · Show 2
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 TimBorquez · Jan 15, 2014 at 07:50 PM 0
Share

i guess if you want the music to keep playing through the scene change do the singleton, otherwise i would just make new things play each time

avatar image CoalCzar · Jan 15, 2014 at 08:29 PM 0
Share

I do want the music to continue playing throughout the level changes without restarting.

2 Replies

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

Answer by Jamora · Jan 15, 2014 at 08:45 PM

Instead of instantiating a MusicScript every time the user enters level1. I would make an otherwise empty scene (think of it as level0) which is loaded when entering the game and that scene first instantiates the MusicScript and then loads Level1. Now, even if the player returns to level1, there won't be another instance of MusicScript instantiated.

One potential problem is that you need to load level0 every time you want to test your sounds. This thread gives a few suggestions on this particular problem.

Comment
Add comment · Show 1 · 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 CoalCzar · Jan 16, 2014 at 05:43 PM 0
Share

Thanks! That does make sense to create a "loading" scene that doesn't get replayed. And thanks for pointing me to the Editor help!

avatar image
0

Answer by Hydrashok · Sep 21, 2017 at 04:42 AM

``` public class MusicPlayer : MonoBehaviour { static MusicPlayer instance = null;

 private void Awake()
 {
     Debug.Log("Music Initialized " + GetInstanceID());

     if (instance != null)
     {
         Destroy(gameObject);
         print("Duplicate Music Killed");
     }

     else
     {
         instance = this;
         GameObject.DontDestroyOnLoad(gameObject);
     }
 }


 // Use this for initialization
 void Start ()
 {
     Debug.Log("Music Initialized " + GetInstanceID());

     
     
 }
 
 // Update is called once per frame
 void 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

21 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

Related Questions

How to get rid of DontDestroyOnLoad? 1 Answer

Singleton and DontDestroyOnLoad question 1 Answer

Using singleton for background music 3 Answers

DontDestroyOnLoad Duplicating Objects When Scene Reloaded 1 Answer

Don't Destroy on Load return to scene Duplicate Music Object 2 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