- Home /
OnTriggerEnter2D --> One Shot Audio Problem
All I'm trying to do is play a sound and disable the sprite when the character runs into it.
So far the code below will play the sound and disable the sprite on TriggerEnter, but the problem is I can run over the trigger again (with the sprite disabled) and the sound will play again. I attempted to stop the sound from playing when the sprite is disabled, but I can't figure it out.
public AudioClip coinHit;
void OnTriggerEnter2D(Collider2D mario) {
if (renderer.enabled = true) {
audio.PlayOneShot (coinHit);
renderer.enabled = false;
Debug.Log ("The sprite was on.");
}
if (renderer.enabled = false) {
Debug.Log ("The sprite was off.");
}
}
}
Answer by lvictorino · Feb 07, 2014 at 08:09 AM
When you disable your sprite, you also have to disable the collider... Then it won't be active anymore so it won't detect the interaction and won't play the sound again.
renderer.enabled = false;
collider2D.enabled = false; // add this here.
I hope it helps.
Oh my gosh. That is so incredibly simple it blew my $$anonymous$$d.
Thank you so much!!
Glad it helped :) - can I ask you to validate my answer as good then if someone has the same problem he will know how to fix it -
Sorry I couldn't find it. I just did though. Thank you again.
Your answer
Follow this Question
Related Questions
OnTriggerEnter problem 3 Answers
Why isn't OnTriggerEnter/Exit working? 2 Answers
Music Zones play music with tags? 0 Answers
Trying to play other objects' sound and animations from Raycast hit 1 Answer
jump on collision enter 1 Answer