Unable to clear text from input field

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.

public class InputWord : MonoBehaviour {
    public TMP_Text inputField;
    private TextMeshProUGUI text;
    private UpdateBoard updateBoard;
    [SerializeField] private string word { get; set; }


    private void Awake() {
        text = GetComponentInChildren<TextMeshProUGUI>();
        updateBoard = GetComponentInParent<UpdateBoard>();
        print("InputWord.Awake inputField " + inputField + " text " + text + "\n");
    }


    public void Clear() {
        print("InputWord.Clear 1  text.text {" + text.text + "} inputField.text{" + inputField.text + "} word{" + word +
              "}\n");
        word = "";
        text.text = "";
        inputField.text = "";
        inputField.SetText("");
        print("InputWord.Clear 2  text.text {" + text.text + "} inputField.text{" + inputField.text + "} word{" + word +
              "}\n");
    }

This is from the console:

InputWord.Clear 1 text.text {} inputField.text{jog} word{jog}
InputWord.Clear 2 text.text {} inputField.text{} word{}

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.

using TMPro;
using UnityEngine;

public class InputWord : MonoBehaviour {
    private TMP_InputField tmpInputField;

    [SerializeField] private string word { get; set; }


    private void Awake() {
           tmpInputField = gameObject.GetComponent<TMP_InputField>();
    }


    public void Clear() {
        print("InputWord.Clear 1  tmpInputField.text {" + tmpInputField.text + "} \n");
        tmpInputField.text = "";
        print("InputWord.Clear 2  tmpInputField.text {" + tmpInputField.text + "} \n");
    }
1 Like