• 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 budin · Jun 11, 2011 at 03:19 PM · audioplaykeybce0017

Audio won't play when key is pressed

I'm pretty new to Unity, and I don't have much experience with scripting, so I was wondering if anyone could help with a problem I have...

I'm trying to get an audio clip to play whenever the key "f" is pressed on the keyboard, as well as an animation. Here's the code:

 function Update () {
      if (Input.GetKeyDown ("f")) {
          animation.Play ("Flight");
          audio.Play("Flight_AudioClip")
      }
 }



So far, the animation works absolutely fine, but the audio refuses to play, and the error box comes up with "Assets/Scripts/Flight.js(5,27): BCE0017: The best overload for the method 'UnityEngine.AudioSource.Play()' is not compatible with the argument list '(String)'."

I have no idea what this means, I've tried replacing "audio.Play" with heaps of other things, looked on the net for templates of scripts to use, but I still can't find any sort of answer...

can anyone help please? I just do not understand...

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

· Add your reply
  • Sort: 
avatar image
2

Answer by FLASHDENMARK · Jun 11, 2011 at 04:55 PM

First of all you will need to add a audio source to the gameObject that is supposed to play the audio clip.

I would do this instead:

 var canPlayAudio = true;
 var waitTime = 1.0; // Change this to the approximate time the audio clip takes to finish.

 var MySound : AudioClip; //Drag your audio clip here in the Inspector.
 
 function Update () 
 {
 AudioController();
 }


 function AudioController();
 {
 if (Input.GetKeyDown ("f") && canPlayAudio)
     {
     canPlayAudio = false;
     animation.Play ("Flight");
     audio.PlayOneShot(MySound);
     yield WaitForSeconds(waitTime);
     canPlayAudio = true;
     }
 }
Comment
Add comment · Show 2 · 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 budin · Jun 12, 2011 at 12:18 AM 0
Share

Thankyou so much, it was very helpful :) I understand now...

But what about if I just want it to play once at a time, because when I press the f key several times, the noise plays over itself, like there are many instances playing in the moment. Right now, I just want the animation and audio to play once at a time, not over itself... do you know what I mean? I tried using Event.Ignore, but the error box scowled at me again...

avatar image FLASHDENMARK · Jun 14, 2011 at 04:14 PM 0
Share

Yes, I do understand your question. And yes I will change the script according to what you just said. :)

avatar image
1

Answer by henry96 · Jun 11, 2011 at 03:38 PM

try to make variable on top to specify which audio!

var myaudio : AudioClip;

then in the fifth line of ur script: audio.Play (myaudio);

if it doesn't work, try

AudioSource.PlayClipAtPoint (myaudio, transform.position);

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
0

Answer by Tom 17 · Jun 11, 2011 at 03:37 PM

The thing is you're using it wrong. You need an instance of an AudioSource first, in the scripting reference this is often called audio. So audio is meant to be a variable. Please review the following pages to get an understanding of how the setup should be:

AudioSource.Play

Unity Manual - Sound

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

what are the input codes for Sony Xperia Play keys? 2 Answers

play audio on touch 1 Answer

Playing audio 1 Answer

Pausing and playing audio on key press? 1 Answer

Can't get my sound to work.. 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