TextField.Focus throwing exception on changing display from none to flex

Hello,

Using latest unity version 2021.2.0f1 (UI Toolkit, runtime). I have an input field that is hidden by default, i.e. style.display = DisplayStyle.None.

Using following code will throw IndexOutOfRangeException. It was working in preview packages. Did I miss something or it’s a regression bug?:

_input.style.display = DisplayStyle.Flex;
_input.Focus();
IndexOutOfRangeException: Index was outside the bounds of the array.
UnityEngine.UIElements.TextCoreHandle.GetLineHeight (System.Int32 characterIndex, UnityEngine.UIElements.MeshGenerationContextUtils+TextParams textParams, System.Single textScaling, System.Single pixelPerPoint) (at <0713f2bf9762492888a6ba6ef8e57ae8>:0)
UnityEngine.UIElements.TextInputBaseField`1+TextInputBase[TValueType].DrawWithTextSelectionAndCursor (UnityEngine.UIElements.MeshGenerationContext mgc, System.String newText, System.Single pixelsPerPoint) (at <0713f2bf9762492888a6ba6ef8e57ae8>:0)
UnityEngine.UIElements.TextInputBaseField`1+TextInputBase[TValueType].OnGenerateVisualContent (UnityEngine.UIElements.MeshGenerationContext mgc) (at <0713f2bf9762492888a6ba6ef8e57ae8>:0)
UnityEngine.UIElements.VisualElement.InvokeGenerateVisualContent (UnityEngine.UIElements.MeshGenerationContext mgc) (at <0713f2bf9762492888a6ba6ef8e57ae8>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)```

Thanks

Summitted a bug report: (1377612) https://fogbugz.unity3d.com/default.asp?1377612_9he14l20te0nrekm

Workaround:

_inputField.schedule.Execute(() => {  _inputField.Focus(); }).ExecuteLater(10);

Thanks, ended up with same approach. But, still want to know it’s a bug or expected behaviour.

1 Like