x


Press Button for Sound

Hello,

I'm trying to develop a goal type game (Fps), Swat type, and i need a script where when you press J It plays the sound (Like SwatTalking.mp3) So the person says "Police on the floor!", Could someone please provide me with a script?, So basicly on input the talking plays.

And if possible, I have 4 voices saying different things, 'Police on the floor' 'Hands up!' 'Police!' 'Get down!' things like that, i need it that everytime you press J It plays one of them, so not in order, just random one every time you press the button,

I hope someone can provide me with a script,

Thank you

more ▼

asked Oct 11 '11 at 08:58 AM

dehan gravatar image

dehan
55 23 30 32

Dude, never say "someone give me a script" in UA - most guys here become infuriated with these (bad) magic words! You're lucky today: @OrangeLightning is in a good mood and provided it without complain (so good that he posted the answer twice!)
But in the future, ask for it in a more generic way, like "could anyone point me the right direction?" - you'll probably receive the script anyway, but without some furious guys downvoting you and eating your reputation.

Oct 11 '11 at 01:11 PM aldonaletto

Sorry, i'm new here.

Oct 15 '11 at 04:01 AM dehan
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

A little search would have easily given you the results, but nevermind. I am in a good mood. :)

First and foremost the easiest is to have an array of audio files and play them one of them randomly when you press the J key.

var swatCommands : AudioClip[]; // Add your audio files here in the inspector.

function Update (){
if(Input.GetKeyDown("j")){
    audio.PlayOneShot(swatCommands[Random.Range(0,swatCommands.length)]); // Choose a random clip here.
    }
}

@script RequireComponent(AudioSource) // Make sure the object has a AudioSource attached.

It should work. I may have spelled length wrong, but the compiler should tell you if I did.

That is properly more or less the basics of randomly choosing a file from an array.

Good luck!

more ▼

answered Oct 11 '11 at 12:08 PM

OrangeLightning gravatar image

OrangeLightning
5.4k 47 57 112

Whoops, doublepost...

Oct 11 '11 at 12:43 PM syclamoth

Yeah I know. Is that a bug? Because I swear I did not press that button twice.

But I cannot delete it because of some bug on UA I guess.

Oct 11 '11 at 12:52 PM Arbiter

I deleted the duplicated answer. It happened to me a few times too... but at least in my case it wasn't a UA bug - it was a bug of mine!

Oct 11 '11 at 12:56 PM aldonaletto

Works Great!, Thank you!

Oct 14 '11 at 05:38 AM dehan
(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:

x842
x798
x525
x35
x5

asked: Oct 11 '11 at 08:58 AM

Seen: 1071 times

Last Updated: Oct 15 '11 at 04:01 AM