- Home /
Audio on collision is not working?
Hello, I would like to attach a script on a trigger Mesh (for example a coin), so that when the 1st person player collides with it, the coin will reproduce a sound.
I've tried this code, I've attached it to the object. Obviously the first person controller is tagged with "Player".
#pragma strict
var myClip : AudioClip;
function Start()
{
audio.clip = myClip;
}
function OnTriggerEnter(otherObj: Collider) { if (otherObj.tag == "Player")
{
audio.Play();
}
else
{
audio.Stop();
}
}
Also i've added an Audio Source Component to the script-attached object, and loaded a WAV sound (2D) inside the Audio Source's Inspector and to the script audio inspector too. Anyway when i collide it doesn't reproduces anything. I have the proof that it collides, becouse there's another script attached to the player, so that when he collides with all these similar objects, tagged with the same name (example: coins) it adds 1 point on the score and prints a Text GUI...
Here's a screenshot, so you can check if everything's fine:
When i collide on it, the player's script for adding a point works, but the script of the audio attached to the object doesn't works, and so it doesn't reproduces any sound...
Please help me, thanks!
Answer by vividhelix · Jul 24, 2013 at 02:25 PM
I think it should be otherObj.gameObject.tag. otherObj is a collider.
Answer by Nerevar1993 · Jul 24, 2013 at 02:45 PM
Ok i solved it, i just had to put a rigidbody on the model :) thanks anyway!
Your answer
![](https://koobas.hobune.stream/wayback/20220613110748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Change Music When GameObject is Grabbed? 1 Answer
PLEASE Help me with this Audio Issue 1 Answer
Audio on Collision code doesn't work? 2 Answers
Stuck on this! Please help me out. 1 Answer