C# bug with global variable

So I am trying to update the variable position when you press a key. I am handling the keypress in update, and the position variable elsewhere. The second functions is called from a string of other functions. KeyPress is a private string. Position is not updating.

`

void update ()

{
	if (Input.GetKey (KeyCode.RightArrow))
		{
			KeyPress = "Right";
		}
	else if (Input.GetKey (KeyCode.LeftArrow))
		{
			KeyPress = "Left";
		}
	else if (Input.GetKey (KeyCode.UpArrow))
		{
			KeyPress = "Up";
		}
	else if (Input.GetKey (KeyCode.DownArrow))
		{
			KeyPress = "Down";
		}
	else if (Input.GetKey (KeyCode.Return))
		{
			KeyPress = "Enter";
		}
	else
	{
		KeyPress = "";
	}
	
}

`

`

IEnumerator Navigate()

{
	int position = 0;
	bool done = false;
	while (done == false)
	{
		Debug.Log("In While");
		if (KeyPress == "Right")
		{
			position++;
		}
		else if (KeyPress == "Left")
		{
			position--;
		}
		else if (KeyPress == "Enter")
		{
			done = true;
		}
		
		if (position > 4)
			position = 0;
		else if (position < 0)
			position = 4;
			
		Debug.Log(position);
		yield return new WaitForFixedUpdate();
	}
}

`

As this statement:

if (position < 4)
    position = 0;

is in the while loop, for every iteration the position is set to 0 again. So it’s just never possible to get a different value. You might want to pull some code out of the while statement and put it after it.