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. Dismiss Notice

How to save text from input fields in a variable?

Discussion in 'UGUI & TextMesh Pro' started by RayDawg, Dec 17, 2014.

  1. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I'm trying to create an input field where the player enters their name and the name is then saved into a variable. I have a canvas setup with an input field, the input field will run a function OnString_PlayerName when the editing is done:

    Code (CSharp):
    1. // Player name
    2. private string playerName = "";
    3.  
    4. // This is run by the input field when done editing
    5. public void OnString_PlayerName()
    6.     {  
    7.         // newPlayer already declared early on
    8.         // setPlayerName stores the player name in another script
    9.         newPlayer.setPlayerName = playerName;
    10.     }
    I don't know how to set the input field to store the text into the playerName variable. Could someone help?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Code (CSharp):
    1.     public void OnString_PlayerName(string value)
    2.     {
    3.         newPlayer.setPlayerName = value;
    4.     }
    Then you can create a new entry on the "End Edit" Event on the input field and choose the OnString_PlayerName function in the drop down . Make sure to assign the gamobject on that the OnString_PlayerName script is living on to the object field.
     
  3. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I tried using the code provided, but nothing is saved to the variable. I did all the steps you listed, but what I don't understand is why pass an argument into OnString_PlayerName? Do I need the private playerName variable?
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Are you quite sure you set it up correctly? fffMalzbier's answer was correct.

    Working example attached:
     

    Attached Files:

  5. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    In the inspector of my inputfield, the OnString function has another input field underneath the dropdown menu. The example you supplied doesn't have such a field in the inspector. Is this normal?
     
  6. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    There are 2 types of parameter for the event structur: Dynamic and Static.
    Static is what you have currently. You can give a fixed input to the function you are calling.
    The dynamic gives you automatically the string that was input into the Input field.
     
  7. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    Oh, there are two possible structures. I didn't see that anywhere in the manual (or I probably looked in the wrong place). It would have been nice to see that there.
     
  8. SuperHB

    SuperHB

    Joined:
    Nov 11, 2014
    Posts:
    1
    How would I use this for an Int?
     
  9. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Code (CSharp):
    1. int x = 0;
    2.  
    3. if (Int32.TryParse(Label.Text, out x))
    4. {
    5.     // you know that the parsing attempt
    6.     // was successful
    7. }
     
    DanSuperGP likes this.
  10. sijpkes

    sijpkes

    Joined:
    Feb 20, 2015
    Posts:
    1
    For anyone else trying to figure this one out, the dynamic and static events are set in the drop down when selecting the function name.

     
    nice_guy_ likes this.
  11. guiliobustiko

    guiliobustiko

    Joined:
    Oct 3, 2022
    Posts:
    5
    Hey i know its been a few years but im wondering how do i use rthis method for 10 input fields