- Home /
How to detect certain text in an input field?
Hello everyone, let me get right to it. So you know how many games have a sort of type in cheatcode to unlock stuff, like the cheatcode system in Turok, Need for Speed, and Conker's Bad Fur Day. So what I'm asking is how can I detect a certain input like the word "Unlocked" and have it do something like a Debug.Log. So pretty much like have the input field there. The player types in something specific like "Unlocked" and a debug.log shows up. If they type something else, an error message shows up in a debug.log. I would show a piece of code, but I don't know how to even start with this input field. Any help would be totally appreciated. I will be clearer if needed.
Answer by Nazirzadeh · Apr 11, 2018 at 12:53 AM
Hi,
public InputField mainInputField;
public string myText;
Then inside your function:
myText=mainInputField.text;
if(myText == "Unlocked")
{
Debug.Log("blah blah");
}
else{
Debug.Log(" Blerg!");
}
Also, take a look at https://stackoverflow.com/questions/28273062/get-text-from-input-field-in-unity3d-with-c-sharp
This worked pretty well for me. Below is my full code that anyone can use.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Scene$$anonymous$$anagement;
public class That : $$anonymous$$onoBehaviour { public InputField mainInputField;
public string myText; // to show what the text is saying
public string NewScene; // a string in which you type in the name of ur scene
public string Cheatcode; // what you want your cheatcode to be
private void Update()
{
Itworked();
}
void Itworked()
{
myText = mainInputField.text;
if (myText == Cheatcode)
{
//below is where you would put what you want it to do
Debug.Log("blah blah");
Scene$$anonymous$$anager.LoadScene(NewScene);
}
// feel free to add this if needed
/* else
{n nj
Debug.Log(" Blerg!");
} */
}
}
So what I did was I made a script called "That" in Unity. Attached it to the InputField game object. Put my cheatcode name and scene name that I wanted it to load. Then click Play, type in your cheatcode letter by letter, and it should work.
Answer by burhanasikwala · Nov 25, 2020 at 09:27 AM
It is actually pretty easy.
using UnityEngine.UI;
class Test : MonoBehaviour
{
public InputField inp;
void Update()
{
if(inp.text == "text")
{
Debug.Log("Player entered text");
}
}
}