I'm trying to make a "Game Over" sound when the player dies and respawns

So i suppose you read the title. The thing is… i’m using a trigger and when the player (a ball) falls in it the game loads the level again. I made it so that the trigger emits a sound (which was stupid) but it plays it before the level loads again so i can’t even hear it. Here’s the code:

#pragma strict

function OnTriggerEnter (other : Collider) {
		Application.LoadLevel("Level02");
		audio.Play();
	}

function Update () {

}

And i want to know how can i make it so this trigger still respawns the ball and i can hear the “Game Over” sound when it loads the level again. Help?

For future posts, please format your code. After pasting, select your code and use the 101/010 button. I did it for you this time.

One easy solution is to just wait to load the next level for some period of time:

#pragma strict
 
function OnTriggerEnter (other : Collider) {
        audio.Play();
        yield WaitForSeconds(1.5);
        Application.LoadLevel("Level02");
}

… or you can wait on the audio:

function OnTriggerEnter (other : Collider) {
        audio.Play();
        while(audio.isPlaying) {
            yield;
        }
        Application.LoadLevel("Level02");
}

If you use the second code, make sure your sound is not looping.

If it plays before the level loads you have the “Play On Awake” property checked. Simply uncheck that and you won’t have that problem.

You really shouldn’t change the level before you call the audio. Also try using audio.PlayOneShot() instead of audio.play().