Calling Event once in OnGUI

I am trying to create a console like application inside my game so I can put cheats in for myself to avoid playing the same parts of the game over and over again. A small issue I ran into, its nothing vital, but something important so I do not have to retype my commands over and over again. I am trying to make it so when I push the “up” arrow, like in the Windows or Linux command prompt, your recent command which you typed appears. I have tried stopping the OnGUI method from calling this section of code more than once; however, I have been unsuccessful. Here is my code:

public int upArrowIndex = 0;

void OnGUI(){
     recentSubmit = false;
        if (Event.current.keyCode == KeyCode.UpArrow && !recentSubmit)
        {

            if (promptLog.Count > 0)
            {

                if (upArrowIndex == 0)
                {
                    prompt = promptLog[0];
                    upArrowIndex++;
                    Debug.Log("Up arrow index" + upArrowIndex.ToString());
                }
                else if (upArrowIndex > 0 && upArrowIndex < promptLog.Count)
                {
                    prompt = promptLog[upArrowIndex];
                    upArrowIndex++;
                    Debug.Log("Up arrow index" + upArrowIndex.ToString());
                }
                else if (upArrowIndex >= promptLog.Count)
                {
                    prompt = string.Empty;
                    upArrowIndex = 0;
                    Debug.Log("Up arrow index" + upArrowIndex.ToString());
                }
                recentSubmit = true;

            }

        }
}

As you can see the multiple executions would put my array out of bounds.

You probably want to check the event type before incrementing like that. Unity - Scripting API: Event.type

So your problem is that you are not checking for the KeyDown event - just checking a key code…

if (Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.UpArrow)