- Home /
Object.DontDestroyOnLoad ?
hello, im having an issue destroying an object (music in the background) after the loading screen. im getting an error when using Destroy(gameobject) because its not loaded in the script for that level... how should i use this to stop the music (destroy) the object or fading the music at certain point? pls help.
you can use for stoping audio.Stop() for mute audio.mute = true; and for pause audio.Pause(); and when you loading next level type DontDestroyOnLoad(this); or DontDestroyOnLoad(gameObject);
thnx for the answer, i've figured it out eventually, and i have used this code to also fade the sound out, but the issue was the "GameObject.Find", which is the way to access and change the object i had lost in the process:
function FadeOut$$anonymous$$enu$$anonymous$$usic () {
var i : int;
if (GameObject.Find("musicScore"))
{
for (i = 10; i > 0; i--)
{
GameObject.Find("musicScore").audio.volume *= .5;
yield new WaitForSeconds (.1);
}
Destroy(GameObject.Find("musicScore"));
}
}
Answer by MacMac098 · Aug 01, 2011 at 08:24 PM
function Start() {
if (GameObject.Find("musicScore") != null) {
for (var i : int = 0; i < 10; i++)
{ GameObject.Find("musicScore").GetComponent(AudioSource).audio.volume *= 0.5;
yield new WaitForSeconds (0.4);
if(i==10){
return;
}
}
Destroy(GameObject.Find("musicScore"));
}
}
ty, i see in your script some code pieces that i didn't know how to write b4 :)
Answer by amit-chai · Aug 01, 2011 at 07:05 PM
thnx for the answer, i've figured it out eventually, and i have used this code to also fade the sound out, but the issue was the "GameObject.Find", which is the way to access and change the object i had lost in the process:
function FadeOutMenuMusic () {
var i : int; if (GameObject.Find("musicScore")) { for (i = 10; i > 0; i--) { GameObject.Find("musicScore").audio.volume *= .5; yield new WaitForSeconds (.1); }
Destroy(GameObject.Find("musicScore")); } }