While holding backspace string doesn't stop being deleted

I have a string called “command” and during the Update function if backspace is pressed it starts a coroutine which is an IEnumerator to delete the last character of the string. When they key is released it’s supposed to stop. If I press backspace and let go straight away it just deletes the last character (like it should), but if I hold it, it deletes all the way back until the string becomes empty then stops. I’d like it to stop when the button is released :confused: Here’s the code:

IEnumerator Deleting () {
		command = command.Remove(command.Length - 1, 1);
		yield return new WaitForSeconds(1.0F);
		while(holdingDelete == true){
			yield return new WaitForEndOfFrame();
			while(command.Length > 0) {
				command = command.Remove(command.Length - 1, 1);
				yield return new WaitForEndOfFrame();
				yield return new WaitForSeconds(0.07F);
			}
		}
	}

And the Update code:

// If the backspace key is pressed while the command isn't highlighted
			if(Input.GetKeyDown(KeyCode.Backspace)){
				if(command.Length > 0) {
					holdingDelete = true;
					StartCoroutine(Deleting());
				}
				highlighting = false;
			}

			// If backspace is released
			if(Input.GetKeyUp(KeyCode.Backspace)){
				holdingDelete = false;
				StopCoroutine(Deleting());
			}

And the bool…

private bool holdingDelete = false;

I just do not know why it keeps going. Once it’s deleted all the text I can type more again like normal because it stops when the string length becomes 0, so I know it can break out of that loop without a problem, so why can’t it break out of the while(holdingDelete == true) part?

You enter the first while loop while holdingDelete is true. Cool, now we do another while loop while Length > 0. You remove your finger from the backspace key halfway through the string. Length is still greater than 0, so it keeps going. Once Length equals zero, it checks if holdingDelete is true. It’s not, so it exits.

You want to check both of those conditions on each iteration through the loop.

while(holdDelete == true && command.Length > 0)
{
   // logic
}