- Home /
Trying to find what player inputed in Text Input field
I'm trying to find what the player typed in, inside of a text input field and store it in a variable. Can somebody please tell me how to do this?
Answer by Legend_Bacon · Feb 19, 2018 at 04:15 PM
Hello there,
The easiest way is to use Unity's built-in events. First, you need to have a script with this function:
private string myVariable = ""; // The variable we want to change based on user input
public void OnFinishInput(string input)
{
myVariable = input;
}
Then, click on your input field in Unity's Hierarchy. You'll find a field "OnEndEdit(String)" at the bottom of its Input Field component. Click on the little "+" below it.
Now, drag the object holding the script with the public void we declared above. Click on the dropdown "No Function", then on the name of your script. Now you see a list of functions you could have this input field call. You'll notice that "OnFinishInput" is there twice, but you want to select the first one (under "Dynamic String") if you want live, user-defined input.
Now, whenever you finish entering text into the input field, it will call the function and set "myVariable" to whatever the input is.
I hope that helps!
Cheers,
~LegendBacon
The final phrase is the wrong way round - it doesn't assign myVariable it sets it. Or you could swap the arguments (ie it "assigns the input to myVariable").
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Input Field text history/previous 0 Answers
How would you go about spell checking player inputted words? 1 Answer
How to change the player while playing?? 1 Answer
Distribute terrain in zones 3 Answers