How to build a basic math quiz

Hello,

I am wanting to build a simple math quiz for my kids. I am not sure what is the best way to build this?

The idea is something like:

Once they have answered correctly, another questions would come up, e.g. 7 circles, with a range of incorrect answers.

The parts that I see are:

  • The question has the name of the shape in it
  • The number of shapes can be random within a range
  • The shape itself can be random (square, triangle, circle etc.)
  • The answers can be random within a range, although one of them must be correct
  • The position of the correct answer must be random each time
  • The number of available answers can be different (usually 3,4,5)

I am not sure what is the best approach to build something like this? Is it best to pre-define the questions in some sort of array? Or, should each question be built as a scene with scenes randomly called? Or can it be truly random and built on the fly?

Any advice you can give would be greatly appreciated.

Kind Regards,

John

i’d go with the “truly random and built on the fly”.

so something like,
StartLevel:

  • pick random correct shape
  • instantiate x amount of those shapes
  • instantiate x amount of wrong shapes
  • generate x amount of wrong answers, add correct answer to the list also, sort them if needed
  • generate answer buttons with those numbers from that list

Hi,

Thank you so much for your answer. I really like the idea of having it random as well. I have watched a few quiz tutorials online and they all seem to favour the array approach, which I feel may be a little limiting.

I am ok with instantiating objects, but how can I get them positioned correctly on the screen? For example, if I had 10 squares, I may want them to be displayed as 2 rows of 5? Normally when I have instantiated objects, they are either in one place or completely random. These would need to be ordered?

Also, is there a way to shuffle a list of answers to randomise which button gets the answer?

Kind Regards,

John

you could put empty placeholders (probably using UI elements, to work with different screensizes)
on those valid positions, and then instantiate objects on their positions (or as a child), if you want them evenly
on 2 rows, can instantiate half on top row placeholders and so on.

yes, can shuffle anything (gameobjects also)

Hi,

That is great, thank you so much. I will have a go at doing something along those lines :slight_smile:

Kind Regards,

John

actually if you only have few different shapes,
i’d just put them there already and activate/deactivate when needed. (saves you from having to instantiate)
or, have empty UI image there, then assign the selected shape image to them.

Hi,

That is interesting, I was thinking about that but there are quite a lot of variations or numbers and shapes.

I have started to write the script to display random shapes in a grid, but when I load, it comes up with the lower number of shapes at the top and the higher number at the bottom.

Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HowMany3 : MonoBehaviour
{
    [SerializeField]
    private GameObject[] prefabs;
    private int randomPrefab;
    [SerializeField]
    private int numPrefabs;

    public int columnLength, rowLength;
    public float xSpace, ySpace;

    private void Start()
    {
        numPrefabs = Random.Range(1, 6);
        randomPrefab = Random.Range(0, 3);

        for(int i = 0; i < numPrefabs; i++)
        {
            Instantiate(prefabs[randomPrefab], new Vector2(xSpace * (i % columnLength), ySpace * (i / columnLength)), Quaternion.identity);
        }
    }
}

This is what it displays:

This is what I want it to display like:
5223839--520712--expected.jpg

Is there a way I can make it do this?

Kind Regards,

John

I belive this is because y grows upwards and your instantiation starts from the bottom of the screen. The easiest way to solve this is probably to set the max y and start decreasing the y value from there.

Hi,

Thank you for your response. I am not sure I understand. How do I set max y and where would I be decreasing from?

Kind Regards,

John

Create [SerializeField] private float maxY (or name it shapesInstantiateVerticalStartingPoint etc for better understanding of your script in the future). In Unity inspector give it a value, for example 7. Change your instantiate vector2 y value like this: maxY -ySpace * (i / columnLength) and try it out.

Hi,

Thank you for the explanation, that works great :slight_smile:

Kind Regards,

John

thank for post

Please use the like button to show your appreciation. Doing that means you’re not necroing the post.

1 Like