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
2
Question by Ravart · Apr 01, 2011 at 11:30 AM · getkeydown

GetKeyDown reacts more than once!

I have a ChatLikeExample. If I press enter the text inside the textfield will be send. My problem is, that GetKeyDown reacts more than once (exactly 4 times), means i send everytime 4 messages instead of one. Of course I could make a bool to check if pressed, but GetKeyDown should do that for me, shouldn't it?

Hope someone can help

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
0

Answer by Justin Warner · Apr 01, 2011 at 11:53 AM

Yes, that'd be fine...

Use the time to really make sure that the player isn't spamming anyways in the first place...

So,

I'd think something like this could work:

If input get key down is enter key, make boolean isSentMessage == true. Also

Then in update() if isSentMessage == true && time > lastMessageTime+5, sendMessageMethod. And in this if statement... Make lastMessageTime updated...

//time is the actual time that has progressed. lastMessageTime is the time that it was when the last message went through. sendMessageMethod is a method for the actual sending of a message. isSentMessage is only down if the player pushed the enter key....

So then only a message can be sent once every 5 sec.

Hope this helps! Any questions?

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 Fhavio · Jul 22, 2019 at 09:39 AM

I'm with the same problem, i'm checking the key press on the Update method and then i call the void() that i want. In this case i'm setting up 2 variables to prevent the loop to enter again but it still enter.

 if (Input.GetKeyDown(KeyCode.G))
 {
     if (bloq != "-")
     {
         //print(s[8]);
         bloq = "-";
         texto.text = "1st TEXT";
         if (s[8] != 1)
         {
             texto.text += "\n 2nd TEXT";
             s[8] = 1;
         }
     }
 }

By default s[8] = 0 and bloq = ""

So in the 1st time a player enters here it will show the 2x TEXTS, looking at the code bloq change to "-" and since s[8] = 0 it will enter the 2nd if statement and change to 1.

My problem is when it loops again it still passes the if statement even with the bloq variable = "-"

Can you guys help me out, just starting with Unity and this is something that is burning my brain :) Apreciate your help

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 Bunny83 · Jul 21, 2019 at 01:14 PM 0
Share

@Fhavio Do not post questions as answers. Your "answer" got already rejected by another moderator since it does not answer the question that was asked.


Are you sure you don'r have that script attached multiple times? $$anonymous$$aybe to different gameobjects? Also Are you sure you don't modify your "bloq" variable elsewhere? Did you try to place a Debug.Log inside your code? Try


 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.G))
 {
     Debug.Log("Pressed G. Detected by go: " + gameObject.name+" at frame #" + Time.frameCount, gameObject);

This will print a log message to the console whenever you press G down. This should only happen once if you have this script only on one gameobject. It also prints the the frame the key down was detected as well as passing the gameobject this script is attached to as context object. When you click on the log message in the console Unity will highlight that object. If you have more than two messages from a single key press it should help figure out what went wrong.

avatar image Fhavio · Jul 22, 2019 at 09:36 AM 0
Share

@Bunny83 Thank you man that was the problem. I associate the script to 4 diferent textboxes, just remove them and associate to just one and it's working fine. 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Button Particle Emission 1 Answer

Can/how do I set up a boolean to continue an animation after button press 1 Answer

GetKey and GetKeyDown are always registered? [Answered] 2 Answers

Trouble getting key down to work 1 Answer

GetKeyDown Enter button not working 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