about Music in a Menu

hello,

i want to play a background music in the menu of a game.

i made the song in “Loop” mode, in their properties.

i used the funtion:

function Update () {
DontDestroyOnLoad (this);
}

for continue playing the song when i enter in a submenu (they are other Scenes).

i did drag the sound source to an empty, and i emparented this script.

i have the problem that when i enter in a submenu, and later i return to the main menu, the song continues playing correcty, but appears other instance of the song playing from the start when i enter in the main menu.

how i can do to not trigger other instance of the song if it is already playing?

It’s the DontDestroyOnLoad() that’s causing it. Each time you come back to your menu scene, an additional copy will be spawned and stay around forever.

If as you say you want the song in all of your submenus which are different scenes altogether, then add a boolean that turns true when you first start playing the song and early exits if one’s already playing and deletes itself.

Obviously, you’ll need to delete the object playing the song manually when you actually enter your game and set the boolean to false.

In addition, you only need to call DontDestroyOnLoad() once, so move it from Update() to Start().