How do i get a sound to play on collision in Unity?

This example we will have a falling object with a sound attached to it.
You want to create an object by dragging a sprite into the scene view and then attach Rigid body and box collider components.

  1. With your object selected, attach a sound by dragging it from the Assets folder into the inspector area. Then you should get an Audio Source component. Untick Play On Awake.
  2. Right click inside the Assets folder and create a new c# Script. Rename it “Hitsound” and double click it so it opens up in MonoDevelop.
  3. Replace your script with the one below and attach this script to your object.

using UnityEngine;
using System.Collections;

public class Hitsound : MonoBehaviour {
    void OnCollisionEnter2D(Collision2D hit)
    {
        if(hit.gameObject.tag == areas )

        GetComponent<AudioSource>().Play();
        
    
         
    }

}


The orange text that says “areas” is the tag name you give to objects you want the object to collide with, like the ground.

So now all you have to do is choose the ground area and attach a tag in the inspector called “areas”

You should now have a sound attached to your object that plays when it hits the ground.

ENJOY!