Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to modify the text of a TextMeshPro input field?

Discussion in 'Scripting' started by Jonhas, Oct 23, 2019.

  1. Jonhas

    Jonhas

    Joined:
    Jul 28, 2019
    Posts:
    46
    I have been making a menu for my game and I need to get the text of what the player put in an input field. I'm not having trouble on defining the text, I know where text object is so like the text the input field displays (the users input), but I'm using TextMeshPro, and using .Text wouldn't work and even when I tried GetComponent that doesn't work. I debated with myself about putting this on UI or Scripting, but I'm scripting something here, so thats why I didn't post this on UI. So anyways, thanks for answering if you did answer!
     
    dan_ginovker likes this.
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @Jonhas you probably tried to get the wrong component (like TextMeshInputField) if you couldn't access text correctly.
    The correct one is TMP_InputField. https://docs.unity3d.com/Packages/com.unity.textmeshpro@2.0/api/TMPro.TMP_InputField.html)

    Code (CSharp):
    1. // Get the component
    2. tMP_Input = gameObject.GetComponent<TMP_InputField>();
    3. // To get the text
    4. string inputText = tMP_Input.text;
    This will give you the current text in the Text Mesh Pro input field, be it the initial text or the current user written one.
     
  3. Jonhas

    Jonhas

    Joined:
    Jul 28, 2019
    Posts:
    46
    Thanks for letting me know, I'll notify you if I have any problems with it!
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Click that like too if the solution worked :)
     
  5. Jonhas

    Jonhas

    Joined:
    Jul 28, 2019
    Posts:
    46
    errrs.PNG

    I did get these errors, I slightly modified the code to work with my own variables too.

    I put it in MonoBehaviour, not in a void or anything.

    Code (CSharp):
    1.     public GameObject name;
    2.     GameObject tMP_Input = name.GetComponent<TMP_InputField>();
    3.     string inputText = tMP_Input.text;
    If you can provide a fix, please reply with one (take your time!), so thanks for trying to help!
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Have you added the required using statement in the beginning of your file, I don't think TMP is accessible otherwise.
    Code (CSharp):
    1. using TMPro;
    I did of course try that code I had and it worked fine over here.

    And Text Mesh Pro input is not of type GameObject. It's of type TMP_InputField.

    i.e.:
    Code (CSharp):
    1. TMP_InputField tMP_Input = GetComponent<TMP_InputField>();
     
    SeerSucker69 and Lotramus like this.
  7. Jonhas

    Jonhas

    Joined:
    Jul 28, 2019
    Posts:
    46
    That exactly works! I didn't think it would, thanks for making those errors disappear!
     
  8. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    You need to figure out always what it is, you can't just try to push them to be a GameObject :)
    But if you want to be more flexible inside a method or where you need to temporarily access something, you can use:
    Code (CSharp):
    1. var tMP_Input = GetComponent<TMP_InputField>();
    ... then the compiler will try to guess the correct thing to put there.
     
  9. misa67

    misa67

    Joined:
    Aug 17, 2018
    Posts:
    5
    Code (CSharp):
    1. public TMP_InputField TMP_IF;
    2. //
    3. TMP_IF.interactable = true;
    4. TMP_IF.text = "";
    For those interested in year 2020 :)
    Seems that property interactable is instantly disabled, solution was to enable it so it text could be updated...
     
    Last edited: Dec 9, 2020
  10. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @misa67 could you edit your post to use code tags? It would be much easier to read and not such an eyesore.
     
    misa67 likes this.
  11. azizkale

    azizkale

    Joined:
    May 18, 2019
    Posts:
    5
    Code (CSharp):
    1. using TMPro;
    2.  
    3. TMP_InputField myInputField;
    4.  
    5. myInputField = GetComponent<TMP_InputField>();
    6.  
    7. myInputField.text = "some text";
     
    danielzbogdanov likes this.