Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to build a basic math quiz

Discussion in 'Scripting' started by johnc81, Nov 27, 2019.

Thread Status:
Not open for further replies.
  1. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    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:
    HowManyShapesMockup.jpg

    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
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,022
    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
     
  3. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    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
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,022
    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)
    https://forum.unity.com/threads/randomize-array-in-c.86871/#post-561135
     
  5. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    Hi,

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

    Kind Regards,

    John
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,022
    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.
     
  7. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HowMany3 : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private GameObject[] prefabs;
    9.     private int randomPrefab;
    10.     [SerializeField]
    11.     private int numPrefabs;
    12.  
    13.     public int columnLength, rowLength;
    14.     public float xSpace, ySpace;
    15.  
    16.     private void Start()
    17.     {
    18.         numPrefabs = Random.Range(1, 6);
    19.         randomPrefab = Random.Range(0, 3);
    20.  
    21.         for(int i = 0; i < numPrefabs; i++)
    22.         {
    23.             Instantiate(prefabs[randomPrefab], new Vector2(xSpace * (i % columnLength), ySpace * (i / columnLength)), Quaternion.identity);
    24.         }
    25.     }
    26. }
    This is what it displays:
    actual.JPG

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

    Is there a way I can make it do this?

    Kind Regards,

    John
     
  8. VSM2018

    VSM2018

    Joined:
    Jun 14, 2018
    Posts:
    16
    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.
     
  9. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    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
     
  10. VSM2018

    VSM2018

    Joined:
    Jun 14, 2018
    Posts:
    16
    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.
     
  11. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    Hi,

    Thank you for the explanation, that works great :)

    Kind Regards,

    John
     
  12. Vasek231

    Vasek231

    Joined:
    Jun 13, 2022
    Posts:
    1
    thank for post
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Please use the like button to show your appreciation. Doing that means you're not necroing the post.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.