- Home /
Play Audio Sound OnTriggerEnter
Hi, I wanted an audio sound to play when the player entered a trigger.
In this case, I wanted a "splat" sound to play when the player touched the ground
Here is the code I have so far...
var SplatNoise: AudioSource;
function OnTriggerEnter(other:Collider)
{
if (other.tag == "Player")
{
SplatNoise.AudioSource.Play();
}
}
I also tried using AudioClip, but that didnt seem to work either, I dont seem to get any errors, but the sound just doesn`t play. Let me know if you need anymore info.
Answer by azmat786n · Dec 01, 2012 at 06:29 AM
var soundFile:AudioClip;
function OnTriggerEnter(trigger:Collider) {
if(trigger.collider.tag=="your_collider_tag") {
audio.clip = soundFile;
audio.Play();
}
}
i hope this is helpful for you.. :)
Answer by Loius · Dec 01, 2012 at 04:59 AM
Another illustration of the importance of maintaining CamelCase for classes and functions versus camelCase for variables.
SplatNoise, as a variable, should be called splatNoise. SplatNoise can't have a non-function member named AudioSource, because that starts with a capital letter. I have no idea how you're not getting an error, but congrats.
AudioSource does not have an AudioSource member. It's already an AudioSource. Just use splatNoise.Play()
Your answer
![](https://koobas.hobune.stream/wayback/20220613084801im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Audio continuation through two scenes 3 Answers
Need help on audio trigger. 2 Answers
Multiple Audio Sources On One Object 0 Answers
How to play sound on destroy (Unity2D) 3 Answers