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 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?