- Home /
Get text from Input field in C#
Hi, 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 inpus Username_field in a C# script?
Thanks :)
This works for me.
using UnityEngine.UI;
public Text Username_field;
//then drag and drop the Username_field
string userID = Username_field.text.ToString();
Answer by LukeAntConroy · Feb 02, 2015 at 09:01 AM
Hi there Bannax, I think this should help with what you are trying to do.
http://stackoverflow.com/questions/28273062/get-text-from-input-field-in-unity3d-with-c-sharp
Answer by systemicgames · Feb 11, 2016 at 03:50 PM
this isn't C# but I think you are trying to access the input text. I'm not quite sure what you mean but I think I might be able to help. Use the variable UI.InputField. When you do that make sure you assign you InputField gameobject to the variable I.E.
var textobject : UI.InputField
function Update () {
if (textobject.text == "hello")
{
Application.LoadLevel("Hello")
}
}
In that example I checked to make sure that the input field text read "hello" after that I unity load a level called "Hello". In your case if you are trying to make a user name you would need to make the InputField text a string.
I looked around the internet for hours without finding any simple solution for checking the InputField text. I tested the above code out in my own game and it worked. Like I said above I'm not sure what you are trying to do but I hope this helps. If you have any problems please feel free to message me and I will do my best to help you solve them.
Answer by levjackie14 · Feb 11, 2016 at 09:35 PM
This works for me.
using UnityEngine.UI;
public Text Username_field;
//then drag and drop the Username_field
string userID = Username_field.text.ToString();
is there a way to use that to fill an input field on the editor?
Answer by Wolverine-X-man · Oct 27, 2020 at 09:31 AM
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);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
Load Scene with password 3 Answers
How to convert text field to input field and back again? 0 Answers