First add an Event System game object to your scene. Then add a canvas. Set the canvas render mode to “Screen Space - Overlay”. Add a panel to the game object the canvas component is on. Then add the text control to the canvas.
Now that you have a text control, you must bind the change event to something so you can capture the value. Create a game object and attach a new script to it. In the script, create a function called something like OnTextChanged(string newValue). Go BACK to the editor, select the text control, scroll down the inspector and locate the “On value change” box. Click the “+” symbol on that to add an event row. Drag your game object over the [None (GameObject)] box. Now select your method (you can actually just use a public string if you’d like) under the DYNAMIC STRING section of the flyouts that show up.
Gotta love the new unity gui! This is why i’m writing my own UI library on codeplex
Ok, for the input field, I would need to attach the above script to the PlaceHolder GO which has a text object attached to it.
That way you are showing a default before the player is typing anything.
Right, understand your issue properly now
You want the player to enter their name in the InputField, then on clicking “Play Now” you want to grab the text they have entered to pass on to the rest of the game.
The InputField component has a “.text” property which holds the value entered, so you just need to grab that and put it in a static variable in a script that isn’t going to get destroyed (or do the poor mans way and store it in player prefs)
To get the entered text from the Input field, you simply:
public static string PlayerName;
// Update is called once per frame
public void SetPlayerName()
{
var inputFieldGO = GameObject.FindGameObjectWithTag("InputField");
var inputField = inputFieldGO.GetComponent<UnityEngine.UI.InputField>();
PlayerNameManager.PlayerName = inputField.text;
}
I’ve simplified it so the script can be placed anywhere in the scene. Obviously you would need to tag the input field better.
The core part is discovering (or attaching the script to) the Input field GO and then grabbing the “InputField” component from it and interrogating the “.text” field to get the entered value.
Could be shortened to just: