Audio collisions

I have an gameobject that has a box collider and an audio source. It also has a Javascript that has a single function:

function OnCollisionEnter(collision : Collision) {
    audio.Play();

}

The audio is not played when a collision happens. What is the problem?

The code looks fine. Make sure that

  • Your game object has a sound attached.
  • You have a collider to collide against.
  • Either one of the object should have a rigidbody.
  • Your camera has a listener.
  • Your camera isn't very far away from the sound.
  • Your speakers are on.

See the collision action matrix at the bottom of this page to see what messages are sent in different circumstances. You can further debug this issue using a print("Collision Detected"); just before audio.Play();

  • Then if you don't get the debug message, the collision isn't happening.
  • If you get the message but no sound, something's still wrong with the sound.

all you have to do is

if(collision.gameObject.FindWithTag("Audio"))// you can rename the tag here to whatever you tag the thing you want to hit to play the attached sound.
{
 audio.Play();
}

make that adjustment, and replace it into your OnCollisionEnter function. really simple. hope this helps!