Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Unable to clear text from input field

Discussion in 'UGUI & TextMesh Pro' started by baconrad, Apr 25, 2024.

  1. baconrad

    baconrad

    Joined:
    Nov 27, 2023
    Posts:
    15
    I have been unsuccessful trying to clear text from an input field. I can see from print statements that the text was removed but it is still displaying. In addition to this, I tried creating an InputField property as I had seen that mentioned in other posts, but was unable to place my Input Field object from the editor. Here is my script. Any help is appreciated. Thanks.

    Code (CSharp):
    1. public class InputWord : MonoBehaviour {
    2.     public TMP_Text inputField;
    3.     private TextMeshProUGUI text;
    4.     private UpdateBoard updateBoard;
    5.     [SerializeField] private string word { get; set; }
    6.  
    7.  
    8.     private void Awake() {
    9.         text = GetComponentInChildren<TextMeshProUGUI>();
    10.         updateBoard = GetComponentInParent<UpdateBoard>();
    11.         print("InputWord.Awake inputField " + inputField + " text " + text + "\n");
    12.     }
    13.  
    14.  
    15.     public void Clear() {
    16.         print("InputWord.Clear 1  text.text {" + text.text + "} inputField.text{" + inputField.text + "} word{" + word +
    17.               "}\n");
    18.         word = "";
    19.         text.text = "";
    20.         inputField.text = "";
    21.         inputField.SetText("");
    22.         print("InputWord.Clear 2  text.text {" + text.text + "} inputField.text{" + inputField.text + "} word{" + word +
    23.               "}\n");
    24.     }
    25.  
    This is from the console:

    InputWord.Clear 1 text.text {} inputField.text{jog} word{jog}
    InputWord.Clear 2 text.text {} inputField.text{} word{}
     
    Last edited: Apr 25, 2024
  2. baconrad

    baconrad

    Joined:
    Nov 27, 2023
    Posts:
    15
    Well, I got it working. Since my gameobject was an instance of TMP_Inputfield, I was able to get the component I needed directly from my current gameobject. On an earlier iteration i was able to drag it into a public exposed field on the editor. Not sure how I stumbled on the fact that I needed TMP_Inputfield. Doesn't seem like it's documented too well. Maybe this will help someone having the same problem.

    Code (CSharp):
    1. using TMPro;
    2. using UnityEngine;
    3.  
    4. public class InputWord : MonoBehaviour {
    5.     private TMP_InputField tmpInputField;
    6.  
    7.     [SerializeField] private string word { get; set; }
    8.  
    9.  
    10.     private void Awake() {
    11.            tmpInputField = gameObject.GetComponent<TMP_InputField>();
    12.     }
    13.  
    14.  
    15.     public void Clear() {
    16.         print("InputWord.Clear 1  tmpInputField.text {" + tmpInputField.text + "} \n");
    17.         tmpInputField.text = "";
    18.         print("InputWord.Clear 2  tmpInputField.text {" + tmpInputField.text + "} \n");
    19.     }
    20.