- Home /
How to stop music from restarting when reloading scene it began?
Hello everyone. I am making a small 2D game, but am having some trouble with controlling the music. I have a menu track and a gameplay track. The menu track loads on a quick scene before the menu pops up. Once in the menu, I can go through all the different menu scenes without the music changing or restarting.
Once the player hits "Play" the gameplay track starts. Upon their death, a new scene comes up where they can choose to "Retry" or "Quit." Here is where the issues come up. If they hit "Quit", it goes back to the menu continuing to play the gameplay track. If they hit "Retry", it goes back to the game, but the gameplay track restarts from the beginning. Does anyone know how to solve this issue?
Note: I am using the popular Singleton solution found from this post http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html.
It is not a track duplication issue. I would like the menu track to go continuously and loop until the Player hits "Play" and then the gameplay track to go continuously and loop until the player hits "Quit", then back to the menu where the menu track begins again. Any help for this would be greatly appreciated. Thank You!
Sounds like you need to check if the audio clip is already playing before starting it
static public void PlayGame$$anonymous$$usic(){
if(instance != null){
if(instance.source != null){
instance.source.Stop();
instance.source.clip = instance.play$$anonymous$$usic;
instance.source.Play();
}
}else{
Debug.LogError("Unavailable $$anonymous$$usicChange component");
}
}
Is that not what this is doing?
@cgraf1 Sorry, Unity never sends me notifications, so I only saw your reply this morning.
If I understand your intent correctly, you'd like the music not to restart when you "Retry" your level.
Your PlayGame$$anonymous$$usic() method always stops the music before playing it. I'm suggesting that you check to see if the music is playing already before you stop and restart it. Like this:
if(instance.source != null && !instance.source.isPlaying){
instance.source.Stop();
instance.source.clip = instance.play$$anonymous$$usic;
instance.source.Play();
}
Your answer
Follow this Question
Related Questions
C sharp cant play Audio 2 Answers
Unity Unable to Reassign Audio Clip 1 Answer
Unity 5 low audio volume 1 Answer
How can I check the scene and play audio accordingly? 1 Answer
Why does the audio sound different than the original ? 0 Answers