Search Unity

TextMesh Pro [ InputField ] GenerateCaret() if no InputSystem NullReferenceException .3359

Discussion in 'UGUI & TextMesh Pro' started by mitaywalle, May 22, 2020.

  1. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253
    Hello! If somehow EvenetSystem disappear or became disabled when InputField is selected / edited, i get NullReferenceException at TMP_InputField.GenerateCaret() 3359 lline

    We switch EventSystem on / off during to custom input for performance reasons.
    Now I'm trying to figure out how to disable InputField Caret generation
     
  2. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253
    Found solution, this component on Input prevent error

    Code (CSharp):
    1. using TMPro;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class InputFieldSafeCheck : MonoBehaviour
    6. {
    7.     private TMP_InputField input;
    8.  
    9.     void Start()
    10.     {
    11.         input = GetComponent<TMP_InputField>();
    12.     }
    13.     void Update()
    14.     {
    15.         if (!input) return;
    16.         if (!EventSystem.current && !input.readOnly)
    17.         {
    18.             input.readOnly = true;
    19.         }
    20.     }
    21. }