- Home /
Question by
DaceProducer_Official · Apr 29, 2014 at 12:29 PM ·
collisiongameobjectrigidbodycollideraudio.playoneshot
Audio.PlayOneSHot won't work?
Hello, I have a game object with an audio source and a script attached to it. The audio should start as soon as it enters collision with a ball, but it just won't play. Here's the script I wrote:
#pragma strict
var hit : AudioClip;
function OnCollisionEnter (theHit : Collision) {
if (rigidbody.collider.gameObject.tag == "Ball") {
audio.PlayOneShot (hit);
}
}
I've also tried replacing the OnCollisionEnter ( ) function with the Update ( ) function, with no success.
Could someone please explain me what I'm doing wrong and point me in the right direction? Thanks.
Comment
does it have an auido source attached to it ? might be the reason. you could do this incase you dont have an audiosource...
var public hit : AudioClip;
var public source = AudioSource;
function OnCollisionEnter (theHit : Collision) {
if (rigidbody.collider.gameObject.tag == "Ball")
{
source = theHit.gameObject.GetComponent<AudioSource>();
if(source != null)
{
source.clip = hit;
source.Play();
}
else
{
source = theHit.gameObject.AddComponent<AudioSource>();
source.clip = hit;
source.play();
}
}
Yes, there is an audio source attached to the game object. I copied the code you gave me, but it gives me an error on lines 8 and 17:
"Unexpected token: )." What might this be?