Playing audio on collision

Hi im trying to play an audio clip when anything collides with my death zone area, i have the audio source on the death zone object and this script attached.

public class SawSoundController : MonoBehaviour {
AudioSource saw;

void Start ()
{
    saw = GameObject.FindObjectOfType<AudioSource>();
}

void Update ()
{

}

void OnCollisionEnter(Collision col)
{
    if(col.gameObject)
    {
        saw.Play();
        Debug.Log("Nurrrr");
    }
}

}

i’ve tried this a few different ways and nothings working, what am i doing wrong ?

using UnityEngine;
using System.Collections;

 //Add this Script Directly to The Death Zone
public class SawSoundController : MonoBehaviour
{
	public AudioClip saw;    // Add your Audi Clip Here;

    // This Will Configure the  AudioSource Component; 
    // MAke Sure You added AudioSouce to death Zone;
	void Start ()   
	{
		GetComponent<AudioSource> ().playOnAwake = false;
		GetComponent<AudioSource> ().clip = saw;
	}    	

	void OnCollisionEnter ()  //Plays Sound Whenever collision detected
	{
		GetComponent<AudioSource> ().Play ();
	}
         // Make sure that deathzone has a collider, box, or mesh.. ect..,
         // Make sure to turn "off" collider trigger for your deathzone Area;
         // Make sure That anything that collides into deathzone, is rigidbody;
}

Hi,

col.gameObject is not a boolean, it’s a GameObject type, it has nothing to do in your if statement.

If you do want ANYTHING going through your collider to trigger the sound, you can get rid of the if statement: whenever OnCollisionEnter is called, it plays the sound.

If you want to filter the gameobjects being able to trigger the sound, you’ll need the if statement but with a boolean. Here’s a possible condition that fits: col.gameObject.tag == "SomeString".

Now make sure that both objects (the one carrying the script and the one that has to trigger the sound) have colliders and that at least one of the two objects has a RigidBody.

First, make sure that your audio doesn’t play on Start by unchecking ‘Play on Awake’. Second, make sure that the collider in your audio trigger is set to Is Trigger. Other audio options such as ‘loop’ are your choice.

@Bman262
I’m saying here that the script is attached to the object through which the sound plays (lets say a ball, whenever the player touches it, sound plays)

I like to create a reference and then in the editor, ‘drag and drop’ my Audio Source inside the ‘yourAudio’ blank

using UnityEngine;
using UnityEngine.Audio;
class Script : MonoBehaviour
{
public AudioSource yourAudio;
void OnTriggerEnter(Collider player)
{
if (player.transform.CompareTag(“Player”)
{
yourAudio.Play();
}
}
}
}

Very simple tutorial here, works in v 2020.1: How to Create a Simple Audio Impact/Collision Trigger with Unity – Async Audio

The code they provide is:

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();
    }

}