7
\$\begingroup\$

Suppose I have two levels. I want the same soundtrack to play on both of them.

At the moment I have an audio source in each level, playing the soundtrack. However, when you move from one level to the other, the soundtrack will of course restart over (because it's a different audio source).

How can I "resume" the soundtrack? Or rather, how can I retain the soundtrack state across levels to have continuous play?

\$\endgroup\$

1 Answer 1

12
\$\begingroup\$

Put the audio source that's playing your music on an object that you've marked as DontDestroyOnLoad

\$\endgroup\$
2
  • 1
    \$\begingroup\$ If you go this route you will need to add a check for duplicates if you can travel from 1-2 and then back to 1 there will now be 2 audiosources. something like if (AudioSpawned==false){AudioSpawned=true;DontDestroyOnLoad(gameObject);return;}else {Destroy(gameObject);} \$\endgroup\$ Mar 4, 2015 at 16:30
  • \$\begingroup\$ What I did was create the Audio Game Object at the game's startup (we got a splash screen scene). This object is kept alive for the rest of the game and no other scenes create one. \$\endgroup\$
    – Saturn
    Mar 9, 2015 at 18:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .