unity on collision enter play sound code example

Example 1: how to play sound on collision unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ImpactTrigger : MonoBehaviour
{
    AudioSource source;

    void Start()
    {
        source = GetComponent<AudioSource>();
    }

    private void OnCollisionEnter(Collision collision)
    {
        source.Play();
    }

}

//https://asyncaudio.com/blogs/tutorials/how-to-create-a-simple-audio-impact-collision-trigger-with-unity

Example 2: play sound on collision unity c#

void Start ()
 {
     saw = GameObject.FindObjectOfType<AudioSource>();
 }
 
 void Update ()
 {
 }
 void OnCollisionEnter(Collision col)
 {
     if(col.gameObject)
     {
         saw.Play();
         Debug.Log("Nurrrr");
     }
 }