How to save text from input fields in a variable?

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:

// Player name
private string playerName = "";

// This is run by the input field when done editing
public void OnString_PlayerName()
    {   
        // newPlayer already declared early on
        // setPlayerName stores the player name in another script
        newPlayer.setPlayerName = playerName;
    }

I don’t know how to set the input field to store the text into the playerName variable. Could someone help?

    public void OnString_PlayerName(string value)
    { 
        newPlayer.setPlayerName = value;
    }

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.

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?

Are you quite sure you set it up correctly? fffMalzbier’s answer was correct.

Working example attached:

1890166–121629–SaveNameToVariable.unitypackage (7.59 KB)

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?

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.

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.

How would I use this for an Int?

int x = 0;

if (Int32.TryParse(Label.Text, out x))
{
    // you know that the parsing attempt
    // was successful
}
1 Like

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.

1 Like

Hey i know its been a few years but im wondering how do i use rthis method for 10 input fields