Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to make a tmp input field to tmp text?

CBKadmCBKadm Member
edited August 2020 in Programming

I’m new to unity and making a game where it asks for the players name using a TMP input field and then displays it onto a TMP text. .. Anyway to do this?

Best Answer

  • JIMMY_VASHI04JIMMY_VASHI04 Member
    Accepted Answer

    TextMeshProUGUI is a class name to reference tye TextMeshPro text ui element


    TMP_InputField is a class name to reference the tmp input field

    Its basically the TextMeshPro ui but in addison you can interect with it

    TextMeshProUGUI tmpT;
    TMP_InputField tmpif;
    
    tmpT.text = tmpif.text;
    

    the tmpT and tmpif is just a name


    And tmpT.text represents the TextMeshProUGUI objects text that you applied from the inspector or if you want to change that from the script


    The tmpif.text represent the text of the inpit field


    So basically when you do this what you are actually saying is


    "Hey unity i want the TextMeshProUGUI object to copy the text from the inputfields text to his own text."


    And tada unity did what you wanted.


    And if you also want to store that value you can use the palyerprefs like

    PlayerPrefs.SetString("PlayerName",tmpif.text);
    


    This means that you are storing a string value to the data of the game so you can use it later.


    Now once you got your name so in the start method you can use it to display the name on the TextMeshProUGUI object. Like


    tmpT.text = PlayerPrefs.GetString("PlayerName","DefaultName");
    


    The DefaultName represents a default value that will be given if the PlayerName string value is not found in the data.and its completely optional.

Answers

Sign In or Register to comment.