How would I get the text input from a text field into a variable?

Hello,

I am a beginner with no epxerience really so please make your explanations imsple and easy to understand :).

I would like a script to run on the click of a button, to grab the text input from a text field, store the input in a variable and then use it as the condition in an if statement.

Please also demonstarate how I would link up the script with the button or any other links I must establish.

Thanks - Any help would be much appreciated :smile: PS: I don’t mind if your answer is in UnityScript or C#.

I bet this is horribly wrong but I’d assume the code would look something like this.

var x = InputField.text;
if (var x = “Hello”){
var score = score + 1;
}

I don’t know, this is clearly my web design Javascipt mode coming out of me even though it is way different for Unity.

2 Likes

looking up the scripting API page for the thing you are trying to work with is generally a good start

http://docs.unity3d.com/Manual/script-InputField.html

InputField iField;
string myName;

void MyFunction()
{
      Debug.Log(iField.text);
      myName = iField.text;
     
      if (myName == "MattCarter")
      {
            Debug.Log("Welcome back Mr. Carter");
      }
      else
      {
           Debug.Log("I Don't Know You!");
      }
}
15 Likes

I’ve got a similar set up in my ChatBox video. From about 14 minutes onwards.

I basically wire up a method to the InputField’s OnChange event, then the button can also reference the stored data.

2 Likes

@5vStudios hello, thank you very much for your help! It doesn’t work though. There is an error. Apparently the name InputField does not exist in the current context?

You have to add the using statement:

using UnityEngine.UI;

at the top of your c# script, unity 5

3 Likes

Wait I think it is working? Please wait and I will inform you of the situation in a minute.

How do I check to see if this debug.log is working? How can I make a pop up in the Android app itself?

Why does y code not work? Here it is…

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CheckAnsL1 : MonoBehaviour {

InputField iField;
string myName;

void MyFunction()
{
Debug.Log(iField.text);
myName = iField.text;

if (myName == “MattCarter”)
{
Debug.Log(“Correct!”, “The word 'cityi is correct!”, “Ok”);
}
else
{
Debug.Log(“Incorrect!”, “The answer was ‘city’.”, “Ok”);
}
}
}

not working ? Please be more specific

1 Like
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CheckAnsL1 : MonoBehaviour
{
public InputField iField;
public string myName;
public string myText;

void MyFunction()
{
Debug.Log(iField.text);
myName = iField.text;

if (myName == "MattCarter")
{
Debug.Log("Correct! The word " + myText + " cityi is correct! Ok");
}
else
{
Debug.Log("Incorrect! The answer was " + myText + " Ok");
}
}
}
1 Like

It says this. When I click on the button to run the script nothing happens. I can’t see if it is working or not?

If the input field contains the correct word, I want it to add a point to a scoring system but I haven’t been able to work out who to create a scoring system regardless of all the tutorials I have watched. I don’t even know how to get the score to display in a text box either and yes I have watched the tutorials on Unity but my application is different as the scripts in the tutorial apply to scenes with completely different coding to mine and I just can’t seem to get anything to work for me?

I also renamed the text input field to iField. I hope that is also correct.

ensure the script is added to a gameobject in your scene then ensure that the function is being called by the button
refer to :
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
if you don’t know how to add the function to the button

also you need to assign the object (InputField) in the inspector on the gameobject that the script is added.

I suggest you watch a couple beginner tutorials :

1 Like

Hello, I added the script to an empty gameobject and linked it to the button. I don’t know what to choose under the menu that you can see in this image?

The image should be attached to this message

2138408--140976--upload_2015-6-1_23-9-59.png

ah ok, first off you have to modify the script and change

void MyFunction()
to
public void MyFunction()

then ‘MyFunction’ should appear in that list that you posted above

1 Like

is this normal even if i entered in the correct answer?

that error means that you did not add the reference to the object in he inspector of the script

1 Like