Create a math game for kids using Unity Scripting

Create a math game for kids using Unity Scripting

Kids games are quite popular these days. And maths is a subject that a student normally fears. How about creating a math game for kids in Unity? It would be learning in a fun way. Let us see a simple way to create a math game for kids using Unity Scripting.

The problem set used in the code includes addition, subtraction, multiplication and simple algebraic equations.

An interesting fact about this code is that there are no inbuilt set of questions. Questions are randomly generated. Thus everytime a kid opens the game, he would get a new question. No repetitions!

Random Addition and Subtraction Question Generator

Note that “questionText” string stores the question as a string.

    // Returns the answer in int to the question stored in string questionText
    string questionText;
    int sumOrMinusQuestion(){
        int operand1 = Mathf.FloorToInt(Random.value * 100);
        int operand2 = Mathf.FloorToInt(Random.value * 100);

        int isMinusOrPlus = Mathf.FloorToInt(Random.value * 2);
        //Is it a + or - question
        if (isMinusOrPlus == 0)
        {
            questionText = operand1 + "-" + operand2 + " = ? ";
            return (operand1 - operand2);
        }
        else
        {
            questionText = operand1 + "+" + operand2 + " = ? ";
            return (operand1 + operand2);
        }
    }

Random Multiplication Question Generator

    // Returns the answer in int to the question stored in string questionText
    int multiplyQuestion()
    {
        int operand1 = Mathf.FloorToInt(Random.value * 50);
        int operand2 = Random.Range(2, 10);
        questionText = operand1 + "*" + operand2 + " =? ";
        return (operand1 * operand2);
    }

Random Multi-Operand Question Generator

Example: (23+ 24)*2?

    int multiOperandQuestion()
    {
        int operand1 = Mathf.FloorToInt(Random.value * 30);
        int operand2 = Mathf.FloorToInt(Random.value * 20);
        int operand3 = Random.Range(1,10);
        int isMinusOrPlus = Mathf.FloorToInt(Random.value * 2);
        if(isMinusOrPlus == 0){
            questionText = "("+operand1 + "-" + operand2 + ")*"+operand3+ " = ?";
            return ((operand1 - operand2)*operand3);
        }else{
            questionText = "("+operand1 + "+" + operand2 + ")*" + operand3 + " = ?";
            return ((operand1 + operand2) * operand3);
        }

    }

Random Algebraic Equation

It would be a simple algebraic equation to find x. Example: 2x+2=4. Find x?

 int algebraicQuestion(){
        int answer = Mathf.FloorToInt(Random.value * 30);
        int operand1 = Random.Range(1,10);
        int operand2 = Mathf.FloorToInt(Random.value * 80);
        int isMinusOrPlus = Mathf.FloorToInt(Random.value * 2);
        if (isMinusOrPlus == 0)
        {
            questionText = operand1 + "*x - " + operand2 + " = " + ((operand1*answer) - operand2)+ ". Find x.";
        }
        else
        {
            questionText = operand1 + "*x + " + operand2 + " = " + ((operand1 * answer) + operand2)+ ". Find x.";

        }
        return answer;
    }

Determining Answer Choices

You can either ask the user to type in the correct answer or you can ask him to chose from a list of options. Here is a code snippet to show three different answers to the user to select from

 //this list contains the 3 different answer for the user.
List<int> answerChoices = new List<int>();
//answer is the correct answer to the question created above. 
void randomAnswerGenerator(int answer)
    {
        correctAnswerIndex = Mathf.FloorToInt(Random.value * 3);
        for (int i = 0; i < 3; i++)
        {
            if (i == correctAnswerIndex)
            { //If this is the index for the correct answer, put correct answer here
                answerChoices.Add(answer);
            }
            else
            {
                int newAnswer = -1;
                int randOpt = Mathf.FloorToInt(Random.value * 2); //Should we add or subtract the random number
                int randBase = Mathf.FloorToInt(Random.value * 5) + 1; //Plus 1 so it doesn't equal zero. Change the 5 to modify how much the generated answers will differ from the real answer
                if (randOpt == 0)
                { //Add
                    newAnswer = answer + randBase;
                }
                else
                { //Subtract
                    newAnswer = answer - randBase;
                }
                answerChoices.Add(newAnswer);
            }
        }
    }

Using the above code, you can create a mini math game in a jiffy.

Happy Coding!

Share This Post