- Home /
Question by
hollym16 · Oct 08, 2013 at 10:02 AM ·
gameobjectdestroydontdestroyonloadloadlevelasync
Destroy Object when scene loads
Hi there. I have a project that uses LoadLevelAsync to preload a level. In the preload level I have a kind of loading screen to let the user know the app hasn't crashed. This level also has audio on it that I've applied DontDestroyOnLoad, so it plays all through.
The trouble I'm having is that once it goes to the next scene (thats been preloaded) I want the audio to stop. I've tried using Destroy (gameObject,5); so it turns off after 5 seconds but I can't predict how fast or slow it will load on every device.
Is there more of an exact way of doing this? Here's my script so far:
function Awake () {
//The GameObject that holds the audio
DontDestroyOnLoad (transform.gameObject);
}
function Start () {
var async : AsyncOperation = Application.LoadLevelAsync ("My Level");
yield async;
Debug.Log ("Loading complete");
}
//GameObject with audio is destroyed after 5 secs
Destroy (gameObject, 5);
Comment