x


Making a sound play only once with a Boolean variable in javascript

I'm very new to coding, and I'm having problems setting my code to play only on the first collision with the box collider.

Here's the code so far.

var Sound : AudioClip;

audio.loop = false;

function OnTriggerEnter(){ audio.PlayOneShot(Sound); }

I've been told to set the variable as a boolean that sets it to play only on the initial collision, but all the help that's available online is concerning things logging in, rather than this.

Any help would be wholeyl appreciated, thanks very much

more ▼

asked May 13 '12 at 02:23 PM

Vial gravatar image

Vial
0 1 1 1

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

1 answer: sort voted first

As being said, add a boolean to test against.

private var hasPlayed : boolean = false;

function OnTriggerEnter() {
    if(!hasPlayed) {
        audio.PlayOneShot(Sound);
        hasPlayed = true;  
    }
}
more ▼

answered May 13 '12 at 02:28 PM

Piflik gravatar image

Piflik
5.4k 15 26 44

Hey Piflik,

That works perfectly, thanks very much. I'm learning more and more about this as this project goes on. Thanks

May 13 '12 at 10:47 PM Vial
(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:

x1031
x986
x527
x241
x28

asked: May 13 '12 at 02:23 PM

Seen: 987 times

Last Updated: May 13 '12 at 10:47 PM