- Home /
Don't destroy a variable when changing scene
How can I keep the music that plays when I change scene, but after some scenes (e.g. the fourth one) the music stops?
Answer by arky25 · Dec 28, 2012 at 01:28 PM
you can use "DontDestroyOnLoad(gameObject);" and use if(Application.loadedLevel == "your level name were you want the music to stop"){ audio.Stop();}
try it out it should work!
Actually, this one works better than $$anonymous$$e...
DontDestroyOnLoad(gameObject);
Application.LoadLevel(nextLevel);
Remove the camera from the other scene and that is it.
One little issue you might get from this way, if your camera has specified behaviours depending on the scene you need to modify the script with both scenes behaviours.
Ok, it worked, but I found an other solution. When the scene that I don't want to play the music loads, It runs this script, that destroy the gameobject that was still in se from the first scene
private var themusic : GameObject;
function Start () {
yield;
themusic = GameObject.Find("$$anonymous$$anager$$anonymous$$usic");
Destroy(themusic);
}
I think you dont need to put the audio on the camera if its a 2D sound.
Answer by Maulik2208 · Dec 28, 2012 at 01:43 PM
it's ok that you are destroying the music else you can simply use isEnabled = false; and your music will be disabled.....This works with gameobjects,3d text etc....as well.....Cheers