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 Lanipoop · Oct 23, 2010 at 08:14 AM · musicsingleton

Using singleton for background music

I'm trying to play background music through multiple scenes by creating a Game Object in my main menu and using the code below:

public class MusicPlayer : MonoBehaviour { private static MusicPlayer instance = null; public static MusicPlayer Instance { get { return instance; } }

void Awake() { if (instance != null && instance != this) { Destroy(instance.gameObject); return; } else { instance = this; }

 DontDestroyOnLoad(gameObject);

}

public void Play() { audio.Play(); }

public void Stop() { audio.Stop(); } }

In my Menu script...

void Start () {
        MusicPlayer.Instance.Play();
    }

The music keeps playing when I go to other scenes, but it stops once I return to the main menu.

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 Jesse Anders · Oct 25, 2010 at 01:13 AM 0
Share

Lani, did you ever solve this problem? What you described sounds kind of like a (possible) Unity bug I've been trying to isolate, so I'd be curious to know how this turned out.

avatar image Lanipoop · Oct 25, 2010 at 01:28 AM 0
Share

Unfortunately, I was not able to fix this. I ended up just making a loading screen and putting the music there. :(

avatar image Lanipoop · Oct 25, 2010 at 01:28 AM 0
Share

Thank you both for the effort, though! $$anonymous$$uch appreciated :)

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by franktinsley · Jun 22, 2012 at 01:15 AM

This is what I finally cobbled together that worked for me.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(AudioListener))]
 [RequireComponent(typeof(AudioSource))]
 class Music : MonoBehaviour
 {
     private static Music instance = null;
     public static Music Instance
     {
         get
         {
             if (instance == null)
             {
                 instance = (Music)FindObjectOfType(typeof(Music));
             }
             return instance;
         }
     }
     
     void Awake ()
     {
         if (Instance != this)
         {
             Destroy(gameObject);
         }
         else
         {
             DontDestroyOnLoad(gameObject);
         }
     }
 }
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
1

Answer by StephanK · Oct 23, 2010 at 08:27 AM

If you use DontDestroyOnLoad(gameMusic) it will "survive" a scene load. In you first script you are destroying the gameMusic object manually every time you load a new scene. If you don't do that it should work.

To use the Singleton pattern just replace the class name with your own. Then add you own code to it. You can now reach this object from every other script using MyClassName.Instance.

Here is the quick and dirty version for you:

[RequireComponent(typeof(AudioSource))] class MusicPlayer : MonoBehaviour { private static MusicPlayer instance = null; public static MusicPlayer Instance { get { return instance; } }

void Awake() { instance = this; DontDestroyOnLoad(gameObject); }

public void Play() { audio.Play(); }

public void Stop() { audio.Stop(); } }

If you attach this script to your gameMusic object the object shouldn't be destroyed and you can start stop your music by calling MusicPlayer.Instance.Play() from any script.

Comment
Add comment · Show 8 · 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 Lanipoop · Oct 23, 2010 at 08:41 AM 1
Share

How do I connect my music game object to the instance?

avatar image StephanK · Oct 23, 2010 at 09:02 AM 0
Share

You don't "connect" it to the Singleton, your music gameObject IS the singleton.

avatar image Lanipoop · Oct 23, 2010 at 09:11 AM 0
Share

Err.. I'm sorry, but where am I supposed to put "game$$anonymous$$usic"? I edited my original post to what I have now.

avatar image Lanipoop · Oct 23, 2010 at 09:22 AM 0
Share

Thank you, spree! It's looking a lot better now. But for some reason, whenever I go back to the menu, the music actually stops. And if I go to a different scene and come back, it'll play again. I changed my original post to show you what I have right now.

avatar image StephanK · Oct 23, 2010 at 11:02 AM 0
Share

That's because every time your menu script starts up it restarts your $$anonymous$$usicPlayer. You should check if the audio is already playing before you start it. You can use audio.IsPlaying for that.

Show more comments
avatar image
0

Answer by cozmoziz · May 03, 2011 at 06:32 PM

Messing around with the code a bit and testing with my game I have found the code below to work. The music plays throughout all scenes and doesn't start and stop when going in and out of the menu. I also just Play the music within the class for when the instance first gets set to this. Don't know if this is sloppy c# coding, but still just getting my feet wet with this language.

using UnityEngine; using System.Collections;

[RequireComponent(typeof(AudioSource))] class MusicPlayer : MonoBehaviour { private static MusicPlayer instance = null; public static MusicPlayer Instance { get { return instance; } }

void Awake() { Debug.Log("instance = " + this); if (instance != null){ Debug.Log("inside the instance != null if statement doing nothing"); return; } else { instance = this; DontDestroyOnLoad(gameObject); Play(); } }

public void Play() { audio.Play(); }

public void Stop() { audio.Stop(); } }

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

1 Person is following this question.

avatar image

Related Questions

How to stop menu music when loading level (1,2,3,..,x) 1 Answer

Different music options in one scene depending on conditions 1 Answer

Music Manager script. Do I want a singleton? 2 Answers

Singletons in music script? 2 Answers

how to make the music better 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