- Home /
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?
Answer by robertbu · Sep 25, 2014 at 06:05 PM
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.
robertbu i tried your scripts but they still didn't work. The sound is heard before the ball respawn. I wanted it like when it falls and respawns then the sound can be heard. I have no problem if the sound isn't co$$anonymous$$g from the trigger i just want to hear it when i respawn.
If you are using Application.LoadLevel() to respawn, then you want to add your audio in the Start() of one of the scripts in that level. That way each time the level is reloaded, the sound will play.
What about the first time the games loads? I will still hear it
Answer by Eaneth · Sep 25, 2014 at 06:16 PM
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().
Your answer
Follow this Question
Related Questions
audio.clip.GetData() returns no data. 0 Answers
Stopping audio source without stopping oneshot clips 0 Answers
Unity Audio Output Data 0 Answers
Avoiding audio transcoding in Unity? 1 Answer
How can I check the scene and play audio accordingly? 1 Answer