Simple question and answer
Hi, I am stuck with pretty basic sounding problem. I want to make question answer game. So when I ask for example 8x8 I would like to write the answer with keyboard after the question. So the player has to press 6 and 4 and numbers would appear on the screen. If a player writes something else the answer is incorrect. I know how to make multiple choice quiz game, but I would like to use writing as answering method. I would really appreciate help on this topic.
Thanks!
Answer by Jessespike · Jun 23, 2016 at 06:17 PM
An InputField can be used to type answers. To get a better idea to do this:
Create 2 UI elements: Text and InputField.
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class MathGuiTest : MonoBehaviour {
 
     public Text label;
     public InputField input;
 
     private float multiplicand, multiplier, product;
 
     // Use this for initialization
     void Start () {
         GenerateNewQuestion();
         input.Select(); //focus cursor to input field
     }
 
     void GenerateNewQuestion() {
         multiplicand = Mathf.Round(Random.Range(0f, 10f));
         multiplier = Mathf.Round(Random.Range(0f, 10f));
         product = multiplicand * multiplier;
 
         label.text = multiplicand + " * " + multiplier + " = ";
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown(KeyCode.Return)) //enter key
         {
             CheckAnswer();
         }
     }
 
     void CheckAnswer()
     {
         float userAnswer;
 
         // if input is valid (is a number)
         if (float.TryParse(input.text, out userAnswer))
         {
             // if the two values are the same
             if (Mathf.Approximately(userAnswer, product))
             {
                 Debug.Log("Correct");
                 GenerateNewQuestion();
                 input.text = "";
             }
             else
             {
                 Debug.Log("Incorrect");
             }
         }
         else
         {
             //input text is invalid, (e.g. text is blank or is not a number)
             input.text = "";
         }
     }
 }
 
 
              Your answer
 
             Follow this Question
Related Questions
[C#] Why is this method called twice? 0 Answers
Static method performance vs non static 0 Answers
Call method of a behaviour from a scripotableobject 0 Answers