- Home /
Removing music gameobject when no-longer wanted.
Hi, I've got a bit of an issue with a gameobject that I've created to play the music in a game I'm developing. I needed the music to continue through two scenes, so I used Don'tDestroyOnLoad(). However, the problem is that now I can't get rid of it. I came across the problem when I was testing the in-game menu I made and I clicked to quit to start menu. When it goes back to the start menu, the music from the previous scene is still playing because of the Don'tDestroyOnLoad(). I'd really appreciate it if someone could explain how I can make it stop the old music. Thanks. :)
Answer by wildex999 · Mar 16, 2014 at 05:23 PM
Couldn't you just explicitly call Destroy() on it when you go to a scene where you no longer need the music?
So in MonoBehaviour.OnLevelWasLoaded on the GameObject playing the music, check if the scene should continue playing music or not.
Sources:
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html https://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html