- Home /
HELP!!! Sound won't play upon Collision! Script looks correct...
Hi guys, This script below isn't working. I've just spent the last 2 hours trying to get it to work but it just isn't happening. My sound file has been converted to a .ogg file, and everything looks correct to me and I get no compile errors. Anyone see what's wrong?
I need it to play the sound when the player touches the object. I am using the FirstPersonControllerPrefab from IslandDemo.
Thanks in advance
var moon : AudioClip; // set this to your sound in the inspector
function OnCollisionEnter (collision : Collision) { // next line requires an AudioSource component on this gameobject audio.PlayOneShot(moon); }
The first thing I'd recommend doing when you run into problems like this is to test that it's even getting to "OnCollisionEnter". Add a '`print("Inside OnCollisionEnter")`' debug message. If it appears, then there's problem with the audio playing (or being heard). If not, you have somewhere else to look. This sort of testing/debugging is usually much more revealing than scouring over your own code for hours. Hope this helps - please update your question with any findings, if you're still stuck!
Also, keep the helpful hints from the manual in $$anonymous$$d: "If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations. Note that collision events are only sent if one of the colliders also has a non-kinematic rigid body attached."
Answer by StephanK · Feb 16, 2011 at 03:10 PM
I you are using the FirstPersonController there probably won't be a Collision Event. Try to use OnControllerColliderHit() instead of OnCollisionEnter.
Answer by tylo · Mar 27, 2011 at 06:34 AM
There is a chart that explains which collision events will fire under certain circumstances.
There are actually three categories of "Colliders". Static (no rigidbody attached), Rigidbody , and Kinematic Rigidbody.
The chart explains under which circumstances each of these will fire.
You can view it here: http://unity3d.com/support/documentation/Components/class-BoxCollider.html
Scroll down to the bottom of the page.
Your answer
Follow this Question
Related Questions
Making a sound using collisions 1 Answer
Sound on collisions 1 Answer
help sound on collision when key down 2 Answers
How to reproduce a sound when my character collide 1 Answer
Trigger respawn after collision? 3 Answers