There is an error with onEndEdit with the inputfield
Hi there, I am trying to get what the user types into input field, but there an error. I have also put using using UnityEngine.UI; as well but with no luck. The error is this:
Assets/InputField.cs(18,20): error CS1061: Type InputField' does not contain a definition for
onEndEdit' and no extension method onEndEdit' of type
InputField' could be found. Are you missing an assembly reference?
public InputField inputfield;
private Dictionary<string, System.Action<string, string>> commands;
protected void Awake()
{
commands = new Dictionary<string, System.Action<string, string>>();
// Add the commands you want to recognise along with the functions to call
commands.Add("new game", OnNewGameTyped);
commands.Add("hello", OnHelloTyped);
// Listen when the inputfield is validated
inputfield.onEndEdit.AddListener(OnEndEdit);
}
private void OnEndEdit(string input)
{
// Only consider onEndEdit if the Submit button has been pressed
if (!Input.GetButtonDown("Submit"))
return;
bool commandFound = false;
// Find the command
foreach (var item in commands)
{
if (item.Key.ToLower().StartsWith(input.ToLower()))
{
commandFound = true;
item.Value(item.Key, input);
break;
}
}
// Do something if the command has not been found
if (!commandFound)
Debug.Log("No command found");
// Clear the input field (if you want)
//inputfield.text = "";
}
private void OnNewGameTyped(string command, string input)
{
Debug.Log("Starting new game");
UnityEngine.SceneManagement.SceneManager.LoadScene(1);
}
private void OnHelloTyped(string command, string input)
{
Debug.Log("Hello my friend !");
}
Answer by Bunny83 · Nov 09, 2017 at 08:53 PM
You named your own class "InputField". So you're hiding the class that is defined inside the UnityEngine.UI namespace. That means your public variable public InputField inputfield;
is actually your own class type and not the UI.InputField.
Either rename your own class / script or use
public UnityEngine.UI.InputField inputfield;
Thanks for that, I can't believe I totally miss that