- Home /
Sound On Collision Not Working
What I'm trying to do is have a sound file play when my player walks through a doorway. I added a game object, removed the mesh, clicked is trigger, added this script::
var audio : AudioClip;
function OnTriggerEnter (other : Collider) {
audio.clip = audio;
audio.Play();
}
And no matter what tutorial I watch or read it all fails. I get no sound no matter what script I use. I have also tried using other file formats for the sound and still nothing. I am completely lost on what I am missing.
Answer by TheEmeralDreamer · Sep 30, 2011 at 08:43 AM
It's a pretty simple fix actually. The only problem with your code is that you would need to tell the audio which sound to play. var audiox:AudioClip=whateversoundyouwanthere;//note:you may need to drag this file to the var in the inspector window audio.Play(audiox);
Altho I recommend using PlayOneShot of the audio usage.
This will enable you to play more than one sound at a time, should you need to. I usually just put it into a function like one of these:
function PlayAudio(newClip:AudioClip,volume:float) { audio.PlayOneShot(newClip,volume); }
function PlayAudioDual(newClip:AudioClip,newClip2:AudioClip,volume:float)
{
audio.PlayOneShot(newClip,volume);
audio.PlayOneShot(newClip2,volume);
}
then you can just call it like this:
PlayAudio(audiox,1.0);
Finally, last but not least:
If you don't get any SOUND at all(like the sounds aren't playing in game), you need to make sure you have an audio SOURCE as well as an audio LISTENER in your scene. Otherwise you won't hear sounds period(just thought it worth mentioning altho I don't think that's your problem.)
Answer by Kacer · Sep 30, 2011 at 08:20 AM
In C# at least there is already a variable that is named "audio", so try writing it like this:
var myClip : AudioClip;
function OnTriggerEnter(other : Collider){
audio.clip = myClip;
audio.Play();
}
remember to assign the audio clip you want to play from the inspector, and remember to put an audiosource on the object you've got this script on.
Answer by Butterzroxx · Oct 01, 2011 at 03:08 AM
Thank you guys for the help I found the issue. My game is in 3rd Person so I had the Listener on the camera instead of the player. So I switched it around and put the Listener on the player and took if off the camera and it worked. I feel like an idiot <.<
Your answer
Follow this Question
Related Questions
OnTriggerEnter gives me error? 2 Answers
Disable Mesh Collider Trigger 1 Answer
Triggering random sound on player 1 Answer
OnTriggerEnter fails to activate 1 Answer
Totally new noob to mecanim and unity itself - Trigger animation? 0 Answers