- Home /
Audio not playing when OnTriggerEnter2D
Audio clip is not playing when OnTriggerEnter2D. Every thing is working right but audio not playing. Here is my script.
public AudioClip coinHit;
void OnTriggerEnter2D(Collider2D other){
audio.PlayOneShot (coinHit);
//Debug.Log ("hello");
Destroy (gameObject);
scorekeeping.count += 1;
}
Destroy () probably destroys your audisource if it's on the same object.
Answer by VoidScreamer · Oct 17, 2014 at 12:16 PM
Don't destroy the object immediately. the audio needs some time to buffer and if you destroy the object, it can't play. and in this case, you need the coin to disappear immediately but you need some time for audio. so add this code : it makes it invisible immediately, but gives some time for audio.
renderer.enabled = false; Destroy(gameObject,0.5);
in this case I would say
Destroy(gameObject, coinHit.length);
to have it destroy when it's done, but you will still get collision detects for this so I would disable the collision for this object right away
Answer by 767_2 · Oct 17, 2014 at 12:15 PM
you destroy it how you expect it to make a sound destroy it after a delay
float delay = 2.0;
yield WaitForSeconds(delay);
Destroy (gameObject);