Search Unity

TextMesh Pro How do I write to an input field (TMP) from script?

Discussion in 'UGUI & TextMesh Pro' started by JmmiP, May 21, 2023.

  1. JmmiP

    JmmiP

    Joined:
    Dec 13, 2022
    Posts:
    1
    In my simulator-style game I'd like to have a menu which displays editable information about the game object that the player has selected. I'd like to have a textbox which updates to display the name of whichever game object is currently selected, but which will update the objects name should the player edit the field.

    To achieve this I just added a TextMeshPro Input Field to the canvas to serve as the text box for the name and implemented the first part of this feature: that is, the name of the selected object will update in On End Edit.

    The next feature I need to implement is what's been giving me trouble. Currently, if I select a different object, the input field will simply display the last thing I typed in. What I'd like is for the text of the input field to update to the name of the object I have selected should I select a new object.

    To do this I just added this line of code:
    Code (CSharp):
    1. myInputField.text = selectedObject.name;
    myInputField is the cached value for the "text" object parented to the "InputField" object in the hierarchy, and it is of variable type TextMeshProUGUI.

    I've searched everywhere online for a solution to this issue and have found nothing. Is there something simple I'm missing?