- Home /
Destroy "Don'tDestroyOnLoad" script in a specific scene ??
I want to Destroy "Don'tDestroyOnLoad" script in some scenes and recall it on the next scene
Hope i can clear what i want..
for an example: I attached "Don'tDestroyOnLoad" script to empty gameObject that's hold some music .. I want the music to continue playing until the player reach specific scene and then stop the script or destroy it and when i pass the scene i want to recall the music back.
Answer by Justin Warner · Mar 30, 2011 at 03:02 PM
http://answers.unity3d.com/questions/2237/easy-question-how-to-disable-script-object
Might help you out... Was thinking when I read the title why you'd use that function... But music does make sense, haha. Hope this helps a little though.
Answer by Tommy · Mar 30, 2011 at 03:05 PM
Maybe you can deactivate the script then activate it again? Here's some code for that:
GetComponent(scriptName).enabled = false;
Then to activate it again use:
GetComponent(scriptName).enabled = true;
To check what scene you'r in use:
if (Application.loadedLevel == "1")
{
//change script here.
}
It will end up something like this:
if (Application.loadedLevel == "1") { GetComponent(soundScriptName).enabled = true; }
else if (Application.loadedLevel == "2") { GetComponent(soundScriptName).enabled = false; }
else if (Application.loadedLevel == "3") { GetComponent(soundScriptName).enabled = true; }
This will make the sound play in scene one, stop playing in scene two, then begin playing in scene 3 again. (Hopefully...)
Answer by Nicolaj Schweitz · Mar 30, 2011 at 07:40 PM
There is no need to Destroy the gameObject with the AudioSource and the DontDestroyOnLoad script.
It sounds to me like you simply should call audio.Stop(), audio.Pause(), or audio.mute = true on the gameObject with the AudioSource.
If you feel daring, you could play around with audio.volume and Mathf.Lerp to create a little fading function that is triggered on the AudioSource over time.