Question by
lukehartsc · Apr 09 at 08:59 PM ·
stringinputfieldintcompare
Trying to compare an input field with an int
Hello
I am building an education game and in the current scene, I am working on I am trying to use an input field as a way for the user to answer the prompt. Currently, I am able to calculate the answer in a separate script and return that as an int. I want to know how to compare that answer with the input field which I am currently saving in another script. Here is the code I have written. I am getting a NullReferenceException: Object reference not set to an instance of an object GetInputOnClick.GetInputOnclick () (at Assets/Scripts/GetInputOnClick.cs:28)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class GetInputOnClick : MonoBehaviour {
//public Button btn;
public InputField inputText;
int text;
public Button btn;
private GameObject answer;
private int submittedAnswer;
private void Start()
{
btn.onClick.AddListener(GetInputOnclick);
answer = GameObject.Find("Answer Input");
}
public void GetInputOnclick()
{
text = int.Parse(inputText.text);
Debug.Log(text);
SolutionForMath solution = answer.GetComponent<SolutionForMath>();
submittedAnswer = solution.answer;
if (text == submittedAnswer)
{
Debug.Log("correct");
}
else
{
Debug.Log("incorrect");
}
}
void CompareAnswer()
{
}
}
Comment