K
K
K1ald2021-09-12 18:08:08
C++ / C#
K1ald, 2021-09-12 18:08:08

Checking the input of the desired number. Why does not it work?

I made a game with multiplication tables. An example is randomly generated and the player needs to enter the correct answer in a field (type: InputField ). The generation works, you can enter numbers in the field, but when you call the method for checking the correct answer from the script by pressing the button, nothing happens, I have been busy with this for a day, it would seem, a trifle, but how much hassle. Below is the script.

public class Maths : MonoBehaviour
{

    [SerializeField] GameObject equationPanel;
    [SerializeField] int firstNumber;
    [SerializeField] int secondNumber;
    [SerializeField] int answer;
    [SerializeField] Text numText;


    public InputField answerInput;

    void Start()
    {
        equation();
        //Debug.Log("Верно!");
    }

    public void equation()
    {
        int firstNumber = UnityEngine.Random.Range(0, 10);
        int secondNumber = UnityEngine.Random.Range(0, 10);
        string taskString = firstNumber + "x" + secondNumber;
        numText.text = taskString;
        answer = firstNumber * secondNumber;
    }

    public void checkAnswer()
    {
        int answerInt = Convert.ToInt32(answerInput);

        if (answerInt == answer)
        {
            Debug.Log("Верно!");
        }
        else
        {
            Debug.Log("Неверно!");
        }
        equation();
    }
}


613e17b56fd7e538271604.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-09-12
@K1ald

You need to figure out what it is answerInputand why it does not turn into a number.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question