- Home /
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
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?
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
@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.
@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
![](https://koobas.hobune.stream/wayback/20220613120135im_/https://answers.unity.com/themes/thub/images/avi.jpg)