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