Play continuous music when swapping between multiple scene in Unity3d

13,796

You need a singleton for this.

The Persistent Singleton

Sometimes you need your singletons to last between scenes (for example, in this case you might want to play music during a scene transition). One way to do this is to call DontDestroyOnLoad() on your singleton.

public class MusicManager : MonoBehaviour 
{
    private static MusicManager _instance;
 
    public static MusicManager instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = GameObject.FindObjectOfType<MusicManager>();
 
                //Tell unity not to destroy this object when loading a new scene!
                DontDestroyOnLoad(_instance.gameObject);
            }
 
            return _instance;
        }
    }
 
    void Awake() 
    {
        if(_instance == null)
        {
            //If I am the first instance, make me the Singleton
            _instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            //If a Singleton already exists and you find
            //another reference in scene, destroy it!
            if(this != _instance)
                Destroy(this.gameObject);
        }
    }
 
    public void Play()
    {
        //Play some audio!
    }
}
Share:
13,796
Sora
Author by

Sora

C# no-jetsu Asp.net no-jetsu Jquery no-jetsu AJAX no-jetsu HTML5 no-jetsu php no-jetsu Telerik no-jetsu you call it magic i call it no-jetsu check out my html5 game using phaser.js i did : http://seekpunk.6te.net/demo.html Feel free to mess around with it and if you need any help in developing html5 games you are welcome to communicate with me on whatsapp :+96170594730 or by email : [email protected] also my github account : https://github.com/balamddine

Updated on June 04, 2022

Comments

  • Sora
    Sora almost 2 years

    I have the following scenario

    1. Main menu scene containing a gameobject with an audio source component
    2. An about us scene

    gameObject script :

    using UnityEngine;
    using System.Collections;
    
    public class ManageMusic : MonoBehaviour
    {
        private static ManageMusic _instance;
    
        public static ManageMusic instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = GameObject.FindObjectOfType<ManageMusic>();
    
                    //Tell unity not to destroy this object when loading a new cene!
                    DontDestroyOnLoad(_instance.gameObject);
                }
    
                return _instance;
            }
        }
    
        private void Awake()
        {
            if (_instance == null)
            {
                Debug.Log("Null");
                //If I am the first instance, make me the Singleton
                _instance = this;
                DontDestroyOnLoad(this);
            }
            else
            {
    
                //If a Singleton already exists and you find
                //another reference in scene, destroy it!
                if (this != _instance)
                {
                    Play();
                    Debug.Log("IsnotNull");
                    Destroy(this.gameObject);
                }
            }
    
        }
        public void Update()
        {
            if (this != _instance)
            {
                _instance = null;
            }
        }
        public void Play()
        {
            this.gameObject.audio.Play();
        }
    }
    

    About us Back button script :

    using UnityEngine;
    using System.Collections;
    
    public class Back_btn : MonoBehaviour 
    {
         void OnMouseDown() 
         {
            Application.LoadLevel("MainMenu");
    
         }
     }
    

    enter image description here enter image description here

    When I click on aboutUs Button the Game Music object keep on playing and I can hear the music but when I return back to the main menu no music is still playing. I can see that the gameobject is not destroyed when I return to the main menu and the audio Listener have the value of the volume set to 1 , but I can't figure out the problem can anyone help me

  • Sora
    Sora over 9 years
    doesn't that code work the exact same as mine ?? , if i want to test your code what should i do ? just replace mine with yours ????and another question , i didn't try your code yet but in my particular case if the singleton is created in the main menu it's script will remain in the about scene but when i go back to the main menu does the singleton persist and keep playing or it will be gone or it will play again ??
  • Varaquilex
    Varaquilex over 9 years
    I'm not sure how your if else code block works but I'll bet on that it doesn't work as you intend. Just go ahead and read about the pattern in the link. Play around with this code snippet and try to change it so that it fits your needs. The game object to which this script will be attached will only be created once the game loads and will not be deleted when changing the scenes, therefore the music will keep playing until you make it stop (like when player changes scene to gameplay and you tell the script to stop playing only then).
  • Varaquilex
    Varaquilex over 9 years
    Can you try to disable 'Play on Awake' on the music, move Play() to if(_instance == null) block and navigate through scenes?
  • Sora
    Sora over 9 years
    Thank you @Varaquilex i managed to fix my code as i made a preload scene and i put my music object in it and i uncheck it's play on Awake than on my main menu i added this script to my start function : if(!ManageMusic.instance.audio.isPlaying) ManageMusic.instance.Play ();
  • Sora
    Sora over 9 years
    i tried your code also disabling play on awake and it worked perfectly thank you for your support appreciate it