- Home /
Audio on Collision code doesn't work?
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
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. 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... (Play on awake is disabled)
Please help me, thanks! :)
Does at least one object in this collision have a rigidbody that is not kinematic? Otherwise, triggers wont work
Answer by shanth · Jul 24, 2013 at 11:46 AM
First of all make sure that whether your game object is having the component Audio source and drag any of AudioClips to the AudioClip object in that Audio source or assign dynamically using scripting. In your script set your audio.clip to any one of AudioClip's object as below.
C#:
Public AudiClip myClip; // drag your audio clip in unity inspector
void Start() { audio.clip = myClip; }
void OnTriggerEnter(Collider obj){ if(obj.tag == "Player"){ audio.Play(); } else { audio.Stop(); } }
JavaScript:
Var myClip : AudioClip; //drag your audio clip in unity inspector function Start() { audio.clip = myClip; }
function OnTriggerEnter(otherObj: Collider) { if (otherObj.tag == "Player") { audio.Play(); } else { audio.Stop(); } }
Answer by Nerevar1993 · Jul 24, 2013 at 12:29 PM
So I've changed the script, attached to the Trigger-Mesh, like:
#pragma strict
var myClip : AudioClip;
function Start()
{
audio.clip = myClip;
}
function OnTriggerEnter(otherObj: Collider) { if (otherObj.tag == "Player")
{
audio.Play();
}
else
{
audio.Stop();
}
}
On the inspector I've put the audio file, and at the end attached this script to the trigger object, also I've added an Audio Source Component on the same object, and I've added the same audio file on it too. Anyway when I run it, it doesn't reproduce the sound... Here's a link to a screenshot, so you can check if everything is fine: Link to Imageshack
Once confirm that the Player tag object doesn't contain the Audio source and Script, that means the object which contains the script and audio source will check if any other object enters into this object. So the other object tag should be the Player.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Audio plays when i click F 1 Answer
Plugin Container for Firefox/Unity Web Player crash whenever I try to load Dead Trigger 2 0 Answers
Realistic game 1 Answer
Help with walking script 0 Answers