- Home /
Music overlaps when returning to initiate scene
Good day, so my current game has a BGM playing at the menu scene which loops for the entire game. But I'm having an issue which is whenever I go back to the menu scene, my code initiates the song once again, so now the game has 2 of the same song playing simultaneously.
function Awake() {
var menuMusic : GameObject = GameObject.Find("MenuMusic");
if (menuMusic) {
Destroy(menuMusic);
}
DontDestroyOnLoad(gameObject);
}
Muting also works on the first initiated BGM only. Please help me with this issue, thanks.
Is the audio object already in the scene or are you adding it with Instantiate?
I have an Audio Source with the song attatched to the audio clip and play on awake ticked.
Which I guess was the problem (ticking play on wake), thanks for asking.
Answer by Tekksin · Jun 13, 2014 at 09:51 PM
i would just do something like:
var played : boolean;
var yourAudioClip : AudioClip;
function Start(){
if(!played){
audio.clip = yourAudioClip;
audio.Play();
played = true;
}
DontDestroyOnLoad(gameObject);
}
didn't test it, but try it out and let me know what happens lol
Your answer
![](https://koobas.hobune.stream/wayback/20220613151437im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Select between two prefabs randomly ? 1 Answer
Simple Instantiate gameObject problem 3 Answers