TextField prints a null reference exception and does not allow the user to type.

Hello!

Im currently working on a unity project (Unity 2021.1.20f1) with UI Toolkit/UI Builder (1.0.0-preview.18) and so far it has been working without problems.

However recently without any changes in the code, on the login page the TextFields for the password does not allow the user to interact with it, the momment its selected this error is printed:

NullReferenceException: Object reference not set to an instance of an object UnityEngine.UIElements.TextCoreHandle.GetLineHeight (System.Int32 characterIndex, UnityEngine.UIElements.MeshGenerationContextUtils+TextParams textParams, System.Single textScaling, System.Single pixelPerPoint) (at Library/PackageCache/com.unity.ui@1.0.0-preview.18/Core/Text/TextHandle.cs:175)
UnityEngine.UIElements.TextInputBaseField1+TextInputBase[TValueType].DrawWithTextSelectionAndCursor (UnityEngine.UIElements.MeshGenerationContext mgc, System.String newText, System.Single pixelsPerPoint) (at Library/PackageCache/com.unity.ui@1.0.0-preview.18/Core/Controls/TextInputFieldBase.cs:759) UnityEngine.UIElements.TextInputBaseField1+TextInputBase[TValueType].OnGenerateVisualContent (UnityEngine.UIElements.MeshGenerationContext mgc) (at Library/PackageCache/com.unity.ui@1.0.0-preview.18/Core/Controls/TextInputFieldBase.cs:735) UnityEngine.UIElements.VisualElement.InvokeGenerateVisualContent (UnityEngine.UIElements.MeshGenerationContext mgc) (at Library/PackageCache/com.unity.ui@1.0.0-preview.18/Core/VisualElement.cs:1464) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

I have gone back to multiple previously succesful builds and they all give out this error, in addition this only applies to the password field, for whatever reason the username field works just fine.

Hi @MaksymShekhovtsov12 ! It seems similar to this issue which is fixed in 2021.2. There’s a backport pending for com.unity.ui-preview.19.

In the meantime, if you are Focusing the TextField on the first frame, try to delay setting the Focus to the second frame. It may fix your issue.

1 Like