Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dehan · Oct 11, 2011 at 08:58 AM · gamesoundmp3

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Oct 11, 2011 at 01:11 PM 1
Share

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.

avatar image dehan · Oct 15, 2011 at 04:01 AM 0
Share

Sorry, i'm new here.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by FLASHDENMARK · Oct 11, 2011 at 12:08 PM

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Oct 11, 2011 at 12:43 PM 0
Share

Whoops, doublepost...

avatar image Arbiter · Oct 11, 2011 at 12:52 PM 0
Share

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.

avatar image aldonaletto · Oct 11, 2011 at 12:56 PM 0
Share

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 $$anonymous$$e!

avatar image dehan · Oct 14, 2011 at 05:38 AM 0
Share

Works Great!, Thank you!

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

'Use SetTriangles instead. Internally this function will convert the triangle strip to a list of triangles anyway.' (27,74) & (130,73) & (177,30) 0 Answers

simple issue just need an expert/experienced advise AUDIO playback 1 Answer

I place an object on the map and when I start the game it's in a different place 1 Answer

getting udp package info inside unity (GlovePIE) 0 Answers

Unity Scripting an interactive game tutorial 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges