- Home /
Playing music while scenes change?
If I start a music track in one level and the next level starts, can I have the track not stop and continue playing through to the next level?
Is this possible in Unity?
Answer by synapsemassage · Apr 18, 2011 at 09:25 PM
Something like this works in my game, attach it to a GameObject that won't be destroyed:
public var SoundClip : AudioClip; private var SoundSource : AudioSource;
function Awake() { DontDestroyOnLoad(gameObject); SoundSource = gameObject.AddComponent(AudioSource); //SoundSource.playOnAwake = false; //SoundSource.rolloffMode = AudioRolloffMode.Logarithmic; //SoundSource.loop = true; }
function Start() { SoundSource.clip = SoundClip; SoundSource.Play(); }
Answer by DaveA · Apr 18, 2011 at 06:40 PM
I'd try this: http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html
So Theoretically if I have an object that I attach a script that would play the game music, If I didn't destroy the object the music would still continue through the scenes? Interesting... But If I load a level and it doesn't load fast enough or I don't have pro and it doesn't load async the music will stop for a brief amount of time? I will test this out thanks for the answer.
I believe that would be correct, but please let us know how it works for you.
Answer by TowerOfBricks · Apr 18, 2011 at 06:48 PM
Though playing music while the scene changes (i.e when loading after Application.LoadLevel call) doesn't work unless you have pro and can load the level async.
$$anonymous$$y scene change times are very fast due to the nature of my game usually 0.1 seconds so I don't think the music stopping for that time would be noticeable, But if I don't destroy the object that is playing the music (Like DaveA suggested) will it restart the song on new load or will it continue playing? I suppose with a good function it could just continue. Thanks for your answer :) I will test this stuff out.
Answer by madflyskills · Jun 12, 2013 at 11:20 PM
You could take a look at SoundManagerPro, it handles that kind of stuff
Your answer
![](https://koobas.hobune.stream/wayback/20220613123614im_/https://answers.unity.com/themes/thub/images/avi.jpg)