I'm trying to get a text inside an inputField in Unity3D
with C#
.
I've placed an inputField in my editor, renamed and tagged in: Username_field.
My question is: How i can get the text inside the InputField
Username_field in a C#
script?
Attach below monobehaviour script to your InputField gameObject:
public class test : MonoBehaviour {
void Start ()
{
var input = gameObject.GetComponent<InputField>();
var se= new InputField.SubmitEvent();
se.AddListener(SubmitName);
input.onEndEdit = se;
//or simply use the line below,
//input.onEndEdit.AddListener(SubmitName); // This also works
}
private void SubmitName(string arg0)
{
Debug.Log(arg0);
}
}
See also below animation:
You can use the "On Value Change" or "End Edit" event of the InputField.
The Unity3D documentation provides more detail on how to use a UnityEvent: http://docs.unity3d.com/Manual/UnityEvents.html
Alternatively, you should also be able to access the Text using the Text property of the Text control that your InputField is attached to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With