C# Number Key For Loop

Hi everyone, I’m trying to make my number keys work with a for loop. But it doesn’t appear to be working. Any idea what’s wrong with it?

 public int someInt; 
   void OnGUI() {
    //for (int i = 0; i < 9; i++){
    if (Event.current.type == EventType.KeyDown) {
    if (Event.current.keyCode >= KeyCode.Alpha1
    && Event.current.keyCode <= KeyCode.Alpha9) {
    someInt = i;
    Event.current.Use();
      }
    }
  }
}

I guess you just want to know which key has been pressed in your someInt variable?

Just do it like this:

public int someInt;

void OnGUI()
{
    Event e = Event.current;
    if (e.type == EventType.KeyDown)
    {
        int key = (int)e.keyCode - (int)KeyCode.Alpha1;
        if (key >= 0 && key < 9)
        {
            someInt = key;
            Event.current.Use();
        }
    }
}

someInt will be set to a number of 0 - 8 (matching key 1 - 9) when one of the Alpha keys is pressed.