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?
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.
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 += "
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