Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

UI Input Field and character limit bug

Discussion in '5.4 Beta' started by amit1532, Mar 12, 2016.

  1. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    Im using an input field and setting the text to some value which is not bigger than the character limit (which is 20).
    and an exception is being thrown:
    ArgumentOutOfRangeException: startIndex + length > this.length

    By looking at the source of the input field im seeing where this is comming from:

    Code (CSharp):
    1.         public string text
    2.         {
    3.             get
    4.             {
    5.                 return this.m_Text;
    6.             }
    7.             set
    8.             {
    9.                 if (this.text == value)
    10.                     return;
    11.                 if (this.onValidateInput != null || this.characterValidation != InputField.CharacterValidation.None)
    12.                 {
    13.                     this.m_Text = string.Empty;
    14.                     InputField.OnValidateInput onValidateInput = this.onValidateInput ?? new InputField.OnValidateInput(this.Validate);
    15.                     int num = this.characterLimit <= 0 ? value.Length : Math.Min(this.characterLimit - 1, value.Length);
    16.                     for (int index = 0; index < num; ++index)
    17.                     {
    18.                         char ch = onValidateInput(this.m_Text, this.caretPositionInternal, value[index]);
    19.                         if ((int)ch != 0)
    20.                             this.m_Text += (string)(object)ch;
    21.                     }
    22.                 }
    23.                 else
    24.                     this.m_Text = this.characterLimit <= 0 ? value : value.Substring(0, this.characterLimit);
    25.                 if (!Application.isPlaying)
    26.                 {
    27.                     this.SendOnValueChangedAndUpdateLabel();
    28.                 }
    29.                 else
    30.                 {
    31.                     if (this.m_Keyboard != null)
    32.                         this.m_Keyboard.text = this.m_Text;
    33.                     if (this.m_CaretPosition > this.m_Text.Length)
    34.                         this.m_CaretPosition = this.m_CaretSelectPosition = this.m_Text.Length;
    35.                     this.SendOnValueChangedAndUpdateLabel();
    36.                 }
    37.             }
    38.         }
    39.  
    This is happening because of this line:
    Code (CSharp):
    1. this.m_Text = this.characterLimit <= 0 ? value : value.Substring(0, this.characterLimit);
    It is happening only when the validation is empty and when the character limit is greater than 0.
     
    tkoknordic likes this.
  2. pfreese

    pfreese

    Unity Technologies

    Joined:
    Feb 23, 2015
    Posts:
    85
    Thanks for this report. This bug was fixed, but it doesn't look like the fix was grafted into the 5.4 beta. We'll try to get this into the next update.
     
    tkoknordic likes this.
  3. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    Thank you!