- Home /
GetKeyUp fires twice?!?
I have a script listening if the user presses a key.
void OnGUI()
{
if (Input.GetKeyUp(KeyCode.C))
{
print("c pressed");
}
}
The problem is that 'c pressed' is printed twice when the user releases the "c" key. Is this a bug or do I do something wrong here?
Answer by Mike 3 · Jun 21, 2010 at 09:54 AM
OnGUI is called multiple times a frame - a minimum of twice (except on iphone where you can disable the layout event)
Use something like this if you need to use OnGUI:
if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.C)
{
print("c pressed");
}
What I did was using the Update function, but thanks for explaining the OnGUI function.
Found out that my function wasnt called in Update as I thought, it was way down in a GUI call. Once I got it moved to Update my problem was solved too.
Answer by Cyclops · Jun 21, 2010 at 02:26 PM
Personally, I prefer to move my input-checking to the Update() function, like so (C# ahead):
bool quitMenu = false;
void Update () { CheckKeys(); } void CheckKeys() { if (Input.GetKeyUp(KeyCode.Escape)) { quitMenu = !quitMenu; } }
By setting a boolean variable, I can then display a Menu in OnGUI() based on quitMenu.
strange, I have tried to build it with Update and a counter, the counter ticks twice within Update too ?
So can you explain because I had it in there and thinking that having all of those if statements for every input would slow down the game so ins$$anonymous$$d i tried on gui, will that slow it down more.
Answer by Jason B · Jul 26, 2011 at 11:33 PM
Since you're saying that it even gets called twice inside of Update, the problem clearly seems to be that you have two objects in your scene running this same script at the same time. I'm not sure how that would have happened, but it seems the most likely culprit.
seems to be firing twice inside my update function also, here is a melee damage script i am using:
function Update(){
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.F)){
$$anonymous$$eleeAttack();
}
}
function $$anonymous$$eleeAttack(){
var colls: Collider[] = Physics.OverlapSphere(transform.position, range);
for (var hit : Collider in colls) {
if (hit && hit.tag == "Enemy"){
hit.Send$$anonymous$$essage("ApplyDamage", meleeDamage);
}
}
}
Attach this to a game object,. then it send apply damage to the following on the object it wants to damage.
function ApplyDamage(damage :float){
if (health > 0){
health -= damage;
healthBarLength -= damage;
}
if (health <= 0){
remove$$anonymous$$e();
}
}
any ideas? if I uncheck the melle damage script while it is running it does no damage, if i check it, it does twice the damage it should.
had the same issue and in my case I found that the script was attached twice once to SceneController object and to my terrain :/ quick way of finding out if that's the case -> detach the script from the object that the script should be attached to and test if the script is still executing if it does well clearly it is attached somewhere else as well ;)