• 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 melonman · Nov 08, 2011 at 10:22 AM · buttonaudio

button hold fires audio every update

Hi Im trying to do a walking sound effect everytime I press a key down which plays and then when I let go the sound stop which I have achieved at the moment by using keyUp and keyDown in void OnGUI.

The problem is if I hold the key down the sound fires over and over again more than once every update i presume.

How would I go about holding the key down and it not firing multiple times just the once until I let go of the button then reset so that whenever I press the key down the sound begins playing from the start again.

I read on unity answers that putting my code in void Update would solve this but when I did it didn't work and I got NullReferenceException:

Unity basically didn't like my if statement. Also tried the following link to fire off the sound which didn't work http://unity3d.com/support/documentation/ScriptReference/Event-button.html

So figure I'll stick with the code I have written but need a way to make the sound just play once when button is held done.

 void OnGUI () 
 {
     if(Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.W)
     {
       audio.PlayOneShot(footsteps_audio);
     {
 {
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
1
Best Answer

Answer by Bunny83 · Nov 08, 2011 at 10:39 AM

That's a design problem. In OnGUI the key events you receive are the wrapped keyboard events. If you hold down a key it is repeated (after the configured delay) with the keyboard repeat-rate (usually set in the OS). Those events are deigned for GUI stuff like a TextField to input a string. For pure event handling you should use the Input class. You need Input.GetKeyDown.

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.W))
     {
         [...]


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 melonman · Nov 08, 2011 at 11:45 AM 0
Share

Thanks Bunny that worked a treat. So going to go with your method it's a shame about the design flaw but as long as this works which it does I'll get over it just fine ;). Thanks again.

avatar image Bunny83 · Nov 09, 2011 at 02:09 AM 0
Share

No problem ;) but by "design problem" i don't ment there's something wrong on Unitys side. OnGUI is ment for GUI usage and in most cases you want / need the typical keyboard-repeat rate, otherwise you would go crazy when you want to move your cursor in a textfield 10 characters to the left.

  • OnGUI is for all kind of GUI related stuff.

  • Update is for your gamelogic

So it's a feature, not a bug ;)

avatar image
0

Answer by cj_coimbra · Nov 08, 2011 at 10:33 AM

I think you can read the audio state with "audio.isPlaying" then just check in your input if the audio already started.

void OnGUI ()
{ 
    if(Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.W) 
    {
        if (audio.isPlaying)
            return;
        else
            audio.PlayOneShot(footsteps_audio);
    }
}
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 melonman · Nov 08, 2011 at 11:44 AM 0
Share

Thanks cj unfortunately I get an error saying that isPlaying cannot be used like a method but i see how it would have worked though.

I tried bunny's method and that works a treat so im going to stick with that.

Thanks again.

avatar image cj_coimbra · Nov 08, 2011 at 11:53 AM 0
Share

Alright, my mistake. It´s "audio.isPlaying" without the ()... if you ever need to use it.

avatar image melonman · Nov 08, 2011 at 02:15 PM 0
Share

Thanks cj will keep this and use this aswell for my parallel project can never have too many solutiions to a problem plus haven't used the isPlaying function before so would be good to try out. Thanks.

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

Audio Play on Touch 1 Answer

How do I run a script when a button is highlighted 3 Answers

Button doesn't play sound after different button press 0 Answers

How to access the OnClick event via Script 1 Answer

UnityWebRequest hangs on isDone but not always 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