Search Unity

Bug NullReferenceException at TMPro.TMP_InputField.SendTouchScreenKeyboardStatusChanged

Discussion in 'UGUI & TextMesh Pro' started by Savidiy, May 14, 2021.

  1. Savidiy

    Savidiy

    Joined:
    Oct 16, 2016
    Posts:
    7
    I get this error on Android 6.0 Meizu M5s, when I recieve "inputField.onSubmit" and hide "inputField.gameObject.SetActive(false)" in it.
    I attach Unity project and apk, which represent this bug.
    I think bug here:
    upload_2021-5-14_14-44-50.png
     

    Attached Files:

    Last edited: May 14, 2021
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,911
    I know that a similar bug was reported a few months back. Please be sure to test with the latest release of whatever version of Unity you are using as well as the latest for TMP.

    By latest, I mean if you are on Unity 2019.4, the latest is 2019.4.26f1 and the latest TMP package for 2019.4 is 2.1.6.
     
  3. Savidiy

    Savidiy

    Joined:
    Oct 16, 2016
    Posts:
    7
    I get this error on Unity 2020.3.6 with the TMP package for 3.0.4 and 3.0.6 version
     
    Last edited: May 14, 2021
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,911
    Downloading your project to take a closer look.

    Do you have steps to reproduce?
     
  5. Savidiy

    Savidiy

    Joined:
    Oct 16, 2016
    Posts:
    7
    I tried to make the example simple. Enter any text in the first field on your mobile device and click done/ok. The mobile keyboard should disappear, the input field should disappear and what gets into the log is displayed on the right. To return the input field, there is a "Restore" button. All the code is in sample.cs
     
  6. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,911
    Thank you for providing the repro project.

    I was able to reproduce the issue which is simply related to the fact that OnSubmit() is called before
    SendTouchScreenKeyboardStatusChanged() which in theory is fine. However, since you are disabling the input field in OnSubmit() this results in OnDisable() getting called which in turn calls DeactivateInputField() which then sets the soft keyboard to null.

    It would seem that calling SendTouchScreenKeyboardStatusChanged() first and then OnSubmit() might be the appropriate change here but I am not entirely sure yet.
     
  7. Savidiy

    Savidiy

    Joined:
    Oct 16, 2016
    Posts:
    7
    In my project, I try to work around this problem. Therefore, I tried to send data and hide inputField not to onSubmit, but to onTouchScreenKeyboardStatusChanged. Unfortunately, if you hide the input field in it, the Exception appears further in LateUpdate. In the picture, I showed the possible location of the error. I think "else if" is appropriate here.
    upload_2021-5-15_13-49-58.png
     
  8. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,911
    I added the "else if" as well as changed the order of the OnSubmit() so that it comes after SendTouchScreenKeyboardStatusChanged()

    Code (csharp):
    1.  
    2. if (m_SoftKeyboard == null || m_SoftKeyboard.status != TouchScreenKeyboard.Status.Visible)
    3. {
    4.     if (m_SoftKeyboard != null)
    5.     {
    6.         if (!m_ReadOnly)
    7.             text = m_SoftKeyboard.text;
    8.        
    9.         switch (m_SoftKeyboard.status)
    10.         {
    11.              case TouchScreenKeyboard.Status.LostFocus:
    12.              SendTouchScreenKeyboardStatusChanged();
    13.              break;
    14.         case TouchScreenKeyboard.Status.Canceled:
    15.              m_ReleaseSelection = true;
    16.              m_WasCanceled = true;
    17.              SendTouchScreenKeyboardStatusChanged();
    18.              break;
    19.         case TouchScreenKeyboard.Status.Done:
    20.              m_ReleaseSelection = true;
    21.              SendTouchScreenKeyboardStatusChanged();
    22.              OnSubmit(null);
    23.              break;
    24.         }
    25.     }
    26.  
    27.     OnDeselect(null);
    28.     return;
    29. }
    30.  
    Let me know if that makes more sense?
     
    Last edited: May 18, 2021
    kyubuns and Savidiy like this.
  9. Savidiy

    Savidiy

    Joined:
    Oct 16, 2016
    Posts:
    7
    Thank you for the changes. When will this be available in Package Manager?
     
unityunity