Single Input Call seemingly being called twice per frame

Good evening, everyone. I was wondering if I could pick your brains about this. So far, the scripts have been working beutifully with exception of this one piece.

I have a button that will go from whatever GUI panel I’m working on to the one before it. The button itself works fine, both in the editor and in the toolset.

The escape key is a little wierd, however. It is being checked at the same time as the button click to see if the key has been pressed, and in the toolset this works as expected. It skips back to the previous panel, just as I’d hoped it would.

The trouble starts when I build and run the application. At that point, the button call still operates properly, but the escape key appears to trigger twice, and ships back past two panels instead of just one.

I even tried taking the escape input check out of OnGUI and putting it by itself in the Update function, and it still has the same behavior for the panel I tried this in. Does anyone have any suggestions as to why this may be happening?

Below is the code for your experienced eyes :slight_smile:

if((GUI.Button(new Rect(150, 575, 200, 50), "<<Back")) || Input.GetKeyUp(KeyCode.Escape)){
    this.enabled = false;			
    GetComponent<GUIChooseRace>().enabled = true;
}

ongui is called X times per frame.
you should never do processing or input checking in there at least not without using Events too, better do input checking in Update

facepalm Wow, rookie mistake. Upon further review, I took the Escape key code check out of all of the affected panels and moved it into the update function. It appears to have solved the problem. I guess OnGUI is called multiple times per frame, rather than only once!

[edit]
Awesome, Dreamora. Thank you for confirming my suspicions! :slight_smile: I will keep that in mind for the future.

In OnGUI, always use Event.current rather than Input.

–Eric