- Home /
Sound will not play in an if statement.
The sound plays fine on awake and in the update loop , however when I try to play it on a collision it will not work.
instruc9er is right, How can I pause for the length of the sound before destroying the object?
The collision is working, I don't know how to wait for the sound to finish then destroy the object.
void OnCollisionEnter(Collision collision) {
if (collision.gameObject == player) {
pickup.Play();
switch(type) {
case 0:
playerprop.health += 10;
break;
case 1:
break;
}
Object.Destroy(gameObject);
}
With $$anonymous$$onoDevelop's debugger or with Debug.Log() calls, you might try to see if the collision function is being called at all, if your GameObject comparison is ever passing, or if that Destroy() call is destroying your AudioSource and preventing it from playing sound.
Your sound might not be playing because, you are destroying the object, right after you set it to play. You have to wait for the sound to play and then destroy the object, otherwise, when the object get's destroyed, theres nothing, that the sound can be played from...
How can I wait for the sound to stop playing. I tried WaitforSeconds(pickup.clip.length), but it won't accept it as an argument.
Answer by eskimojoe · Oct 31, 2013 at 02:17 AM
Try this instead:
if (collision.gameObject.name == player.name) {
}