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 /
This question was closed May 12, 2013 at 03:26 PM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by VipDeveloper · May 12, 2013 at 11:25 AM · audiowith

Need help with playing audio on click.

Hey guys just a simple question here, i've made a gun and i want it to shoot when i press Fire1 ( Yes i know its fire1 :D ) so the script would be something like this?

 var handGunShot : SoundClip;
 
  
 
 function Update(){
 
 if (Input.GetButton("Fire1"){
 
 //shoot the bullet
 
 audio.PlayOneShot(handGunShot);
 
 }
 
 }


I Can change the handgunshot to my audio file right?

Thanks.

  • Vince

Comment
Add comment
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

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Jeejo · May 12, 2013 at 12:21 PM

This should work:

 var handGunShot : AudioClip;
 
 function Update () {
      if(Input.GetMouseButtonDown(0)) {
           audio.PlayOneShot(handGunShot);
      }
 }
Comment
Add comment · Show 7 · 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 VipDeveloper · May 12, 2013 at 12:25 PM 0
Share

Should i change the handGunShot to what my mp3 file name is?

avatar image Jeejo · May 12, 2013 at 12:26 PM 0
Share

Also, you shouldn't put the sound directly into the AudioSource. When you play the game the script will work it out by itself. (NOTE: This is just in case, I'm not sure if you have been doing this, but I just wanted to post it in case you weren't.)

avatar image VipDeveloper · May 12, 2013 at 12:28 PM 0
Share

Can you give me more information on what to do cause now im not so sure anymore...

Thanks.

avatar image Jeejo · May 12, 2013 at 12:28 PM 1
Share

No, you don't have to. You just drag the audio clip into the inspector (under the script, next to Hand Gun Shot).

avatar image VipDeveloper · May 12, 2013 at 12:36 PM 0
Share

$$anonymous$$y sound isn't called handGunShot but $$anonymous$$P5$$anonymous$$ so i have a gun called Gun Prefab should i drag the script with sound into the gun and then it should work? If so please say because i really need help.

Show more comments
avatar image
1

Answer by Sanky · May 12, 2013 at 12:12 PM

var soundPlay : AudioClip;

function Update() { if(Input.GetMouseButtonDown(0)) { audio.Play(); } }

Abouve script just attach your object and turn of play on awake Boolean from audioSource.

Comment
Add comment · Show 3 · 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 VipDeveloper · May 12, 2013 at 12:20 PM 0
Share

Can you help me a little bit more? I Did the javascript made it and put the sound in. Now i put the script inside my gun model and it still plays on start of game and doesn't when i click in screen. :/

Thanks

  • Vince

avatar image Sanky · May 12, 2013 at 12:32 PM 1
Share

have you removed 3d sound option from that sound clip and also you have to attach audio source component to your gun object then add that clip to their then the clip will plays well.

avatar image Fattie · May 12, 2013 at 03:25 PM 0
Share

also LOO$$anonymous$$ at the inspector, and TURN OFF automatic play. that's what is making it play on load.

avatar image
1

Answer by malekbakeer · May 12, 2013 at 12:56 PM

just copy and paste this

 var _sound : AudioClip;
          
 function Update(){
          
     if (Input.GetButton("Fire1"){
          
         audio.Play("put your sound name here");         
     }

then put the script in your character then chose your soundclip that u wanna play

Comment
Add comment · Show 1 · 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 dimic4 · Apr 23, 2020 at 10:22 PM 0
Share

Hi,

so, i'm working on a game, and i have a dialog between two people. I already make, that when the player click on button next, the next dialoge is shown. i have 9 dialogs, so the player must press 9 times on button next, to end up dialoge.

The problem i have now, that i want when text is shown the audio clip(i recorded voices and export as mp3 in audacity) is on volume too. i have 9 audio clips and i can't pair it with texts.

any solutions?

Thanks

Follow this Question

Answers Answers and Comments

19 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Audio on Collision code doesn't work? 2 Answers

Change Music When GameObject is Grabbed? 1 Answer

I can't figure out how to fix an "Expressions in statements must only be executed for their side-effects." problem. 1 Answer

I need help scripting a melee attack 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