Backspace in limited length textfield causes ArgumentOutOfRangeException

Hello everyone,

Tried using GUI.TextField with limited length of 4, but when I type ‘1234’ and type any other number then try using backspace to delete the string, it throws the said error.

ArgumentOutOfRangeException: startIndex + count > this.length

Parameter name: count

Following the stacktrace it seems UnityEngine.TextEditor.Backspace () gets called but the string length already changed and so the error occurs. Tried unsuccessfully to get around it but not luck so far, only by negating the backspace event when it reaches capacity, but then no more backspacing.

Happened for me too.

I fixed the issue in our program by catching the exception and reducing the String-Length by 1 if it occurs.

try
{
   newValue = GUI.TextArea(rect, stringToEdit, stringLength, style);
}
catch (System.Exception e)
{
   if (e is System.ArgumentOutOfRangeException)
   {
      newValue = newValue.Substring(0, newValue.Length - 1);
   }
}