Trouble understanding For Loop input

So I have understood the way that the “For” Loops work (cause of my experience from Java), but I still have a question that bothers me. Does the statement ( if (Input.GetKeyDown(KeyCode.Alpha1 + intex))) receive a number from the keyboard, on every state/step of the for loop? For example, the for loop is executed, and on our first “if” we type the number 8, which is invalid in our example. so it returns false and the Intex is adding 1. Now we are at the 2nd state of the loop. Does it receive input from the keyboard (again) or does it keeps the first number that we typed and as a result the for loop just ends after checking in all the other states?? I know that the whole ManageState() method happens on each frame, but I have trouble fully understanding it. Can someone explain it a bit more? :slight_smile:
Thanks in advance.

Hi Theofanis,

Welcome to our community! :slight_smile:

Unity does not receive input from the keyboard multiple times just because we execute a for-loop. That would be a waste of resources. Instead, Unity checks if a key was pressed during this frame and sets the respective variable/property to either true or false. We call GetKeyDown, pass on a KeyCode, and Unity checks the value of the respective variable/property. At least, this is what I think is going on behind the scenes. Most of Unity is not open-source, so I don’t know if I’m right.

1 Like

Thank you for replying! I think I have a more clear understanding now.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms