- Home /
Audio won't play on hit
Hello. I'm trying to play audio when the character dies. Here is what I have.
private var finish = false;
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.gameObject.tag == "KILL"){
finish = true;
}
if(finish){
Death();
}
}
function Death(){
Camera.main.SendMessage("fadeOut");
audio.Play();
LevelLoad();
}
function LevelLoad(){
yield WaitForSeconds(2);
Application.LoadLevel(1);
finish = false;
}
I've tried moving audio.Play()
to different parts of this code but it won't work. Thanks in advance.
Are you getting any errors or is it just plain not playing?
If I increase my volume very high, I hear a constant tone. Almost as if its starting to play the the beginning of each frame. I don't understand why it would be doing that...
Answer by aldonaletto · Dec 06, 2011 at 12:44 AM
OnControllerColliderHit happens all the time due to the collision of the character with the ground, and this is calling Play each Update after the character hits the KILL object. Change your code a little to ensure that Death will be called only once:
private var finish = false;
function OnControllerColliderHit(hit : ControllerColliderHit) { if (hit.gameObject.tag == "KILL" && finish == false){ finish = true; Death(); } } This will also avoid calling LevelLoad multiple times, what can cause a disaster (it's a coroutine, and each call to LevelLoad starts a new instance of this coroutine).
THAN$$anonymous$$ YOU!! I've been at this for 4 hours now. I knew it had to be something really simple... /sigh... lesson learned.
Your answer
Follow this Question
Related Questions
Play "Fail Laugh" on Respawn 3 Answers
OnTriggerExit stop audio 1 Answer
Audio Issue, Plays with PlayOneShot but not Play 0 Answers
video texture plays too fast when adding audio 2 Answers
Background Music 2 Answers