- Home /
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.
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);
}
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.
The problem is that I want the music manager to be insideWhen you're destroying the old one and creating anew one
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!
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);
}
}
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
![](https://koobas.hobune.stream/wayback/20220612192805im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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