x


Activating Audio When Hitting a collider

Okay HEYYYY i was just wondering how i would activate an audio source when hitting a collider i thought it would be like the code below but it does not seem to work :/

function OnTriggerEnter (Hit : Collider)

{ AudioSource.Play("Name Of the Audio"); {

This Method doesn't Work And i was wondering if somebody could help, or point me in the right direction :D thanks for reading :D

more ▼

asked Feb 28 '12 at 09:08 PM

benjimazza gravatar image

benjimazza
102 23 34 37

Instead of OnTriggerEnter, try OnCollisionEnter.

Feb 29 '12 at 08:50 AM Kleptomaniac
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Im reasonably new to coding, but maybe try changing

AudioSource.Play("Name Of the Audio");

to

audio.Play();

and add an audio source to the object and uncheck 'loop' and 'play on awake'

not too sure, hope this helps.

more ▼

answered Feb 29 '12 at 12:00 AM

GoThXoRd gravatar image

GoThXoRd
31

(comments are locked)
10|3000 characters needed characters left

First, make sure you close the function with a closing bracket }, not another open bracket. Next, as GoThXoRd mentioned, you should use

audio.Play();

with the audio on the game object (you can just drag it on in the inspector, even).

If you are still having troubles, the following must be true for that function to work:

If you don't want the collider to be a trigger, use OnCollisionEnter()

more ▼

answered Feb 29 '12 at 09:22 AM

Alec Slayden gravatar image

Alec Slayden
1.2k 1 4 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1937
x1024
x531
x25

asked: Feb 28 '12 at 09:08 PM

Seen: 852 times

Last Updated: Feb 29 '12 at 09:24 AM