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 potu1304 · Jul 25, 2012 at 12:33 PM · c#inputaudiosound

Play Sound on Fire (C#)

Hello!

I would like to play a gun sound when I click the left mouse button. How should I do this in C#? With AudioSource or AudioClip?

Greetings

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

2 Replies

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

Answer by AlucardJay · Jul 25, 2012 at 01:22 PM

Any audio needs an audioSource to play. Whether you assign audio in the Inspector, by script with audioClip, PlayOneShot, PlayClipAtPoint.

Which of these methods you use depends on how you want to use the sound. And personal preference or coding style. For a gun firing you could play the audio or PlayOneShot, and for the bullet ricocheting at the point of impact use PlayClipAtPoint.

from : http://docs.unity3d.com/Documentation/Components/class-AudioSource.html

You can play a single audio clip using Play, Pause and Stop. You can also adjust its volume while playing using the volume property, or seek using time. Multiple sounds can be played on one AudioSource using PlayOneShot. You can play a clip at a static position in 3D space using PlayClipAtPoint.

But for any Audio , you Need an AudioSource =]

Some reference links :

http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.html

http://docs.unity3d.com/Documentation/ScriptReference/AudioClip.html

http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayOneShot.html

http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayClipAtPoint.html

Here's a C# example of PlayOneShot : http://answers.unity3d.com/questions/278058/how-to-add-a-sound-to-this-script.html

And a JS example of changing the audioClip to be played (scroll down to my answer) : http://answers.unity3d.com/questions/250261/press-key-p-the-object-will-random-to-play-music.html

If you need any clarification from all this info, let me know =]

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

Answer by kwokman · May 29, 2015 at 09:13 AM

You can try the simple:

AudioSource audio = gameObject.AddComponent < AudioSource > ();

audio.PlayOneShot ((AudioClip)Resources.Load ("gunshot1"));

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 oranmooney · Jul 03, 2017 at 02:28 PM 0
Share

error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.gameObject'

@kwokman

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

9 People are following this question.

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

Related Questions

How can i set audio mixer groups volume to be used with ui slider when 0 is lower and 80 louder ? 1 Answer

Multiple Cars not working 1 Answer

Play audio from directory? 0 Answers

Audio Scripting 1 Answer

How to start a sound not from it's start ? 2 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