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 Dreave · Nov 03, 2011 at 09:09 PM · errorbuttonsoundpress

Play sound on button press?

I have wrote this script which I think is supposed to play a sound when the LMB is pressed, but I have lots of errors which I'm guessing means this script isn't possible. Can any take a look and help me out?

function Update () {

if(Input.GetButtonDown("Fire1")){

   audio.Play("sniper rifle gunshot");
   

}

}

Comment
Add comment · Show 1
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 DaveA · Nov 03, 2011 at 09:36 PM 0
Share

It's totally possible and easy. Please post your code so we can see what's gone wrong.

1 Reply

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

Answer by aldonaletto · Nov 03, 2011 at 10:09 PM

audio.Play() only plays the sound assigned to audio.clip - you can't pass the file name. The object to which this script is attached must have an AudioSource, and the sound must be imported with menu option Assets/Import New Asset... and assigned to the AudioSource clip property (Audio Clip, in the Inspector).
If you have different sounds to play in the same object, you can change the clip property and use Play, or use PlayOneShot(sound), where sound is an AudioClip variable.
Use audio.Play without any arguments:

 audio.Play(); // play the current audio.clip AudioClip

Or define the AudioClip variables and play one of them with audio.PlayOneShot:

 var sound1: AudioClip; // drag the sound 1 here
 var sound2: AudioClip; // drag the sound 2 here
 ...
 audio.PlayOneShot(sound1);
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 Dreave · Nov 04, 2011 at 05:16 PM 0
Share

do you mean like this because I have an error saying "no appropriate version of unityengine.audiosource.play for the argument list (string) was found" I have no idea what this means this is the script.

var sound : AudioClip;

function Update (){

if(Input.GetButtonDown("Fire1")){

audio.Play("sniper rifle gunshot");

}

}

avatar image aldonaletto · Nov 04, 2011 at 05:33 PM 1
Share

That's what I said above: Play doesn't accept a file name - define the sound in the AudioSource at the Inspector and use just Play(), without any argument. An alternative is to use an AudioClip variable and play it with PlayOneShot. The example below show both cases - use only one of them and delete the other!:

var sound : AudioClip; // drag the sound here

function Update (){ if(Input.GetButtonDown("Fire1")){ // you can use PlayOneShot to specify the sound... audio.PlayOneShot(sound); // or you can define the sound in the Inspector // and use Play like this: audio.Play(); } }

avatar image The-Arc-Games · Nov 05, 2011 at 05:42 AM 0
Share

Great answer as usual :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Screetching Sound While Walking? Why? 0 Answers

Play sound, while press any key 3 Answers

how to make the character look at the object and when clicked on mouse0 the object is removed with sound? 1 Answer

how to make the character look at the object and when clicked on mouse0 the object is removed with sound?(3D) 1 Answer


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