Check for Key Press Freezing.

So I have a problem where my game freezes up. I am checking for key presses in update, and setting a variable to know which key was pressed. Then I have another function that continues to check to see the value of said variable. It then determines what to do based on what key you press. So here is my update,
`

void update ()
{

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

`
and here is my function that responds to the key presses.

`
string Navigate()
{

	string choice = "";
	int position = 0;
	bool done = false;

	while (done == false)
	{
		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);
	}
	return choice;
}

`

This is only part of my script, but when I comment out this part it works fine. I have another function that calls Navigate.
Why is this hanging up? I don’t see a problem.

Your while loop never ends.

    while (done == false)
    {
         //done = true; //This missing in your code. so while never ends and stuck.
    }

as yashpal says, you enter the loop but it never ends,

take the while(done == false) out

I would store the position in a temporary variable and then commit when done is true

I solved this by using a coroutine instead. Since I needed the function to continuously update I switched the type. Simply removing the while as suggested would make it run, but would kill my logic. So here is the final code.

`

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();
	}
}

`

And then just call StartCoroutine(Navigate()); to run it.