Advice of storing sets of questions and answers for a trivia game

Hi guys :slight_smile:

In an attempt to get better and more comfortable with coding in c# and unity, I’m trying to work on a small trivia/quiz game.

One thing I’m having a hard time figuring out is a good way to store questions and answers.
I should be able to figure out how to randomize the order of the questions, but how do I do so while ensuring that the answers are following the same randomization pattern?

Currently I’ve laid out the UI so that there the questions are displayed in a panel while the answers are represented in 3 buttons. I don’t know if I’m making things more complicated by doing so, but any advice would be greatly appreciated.

I should add that I have a limited C# knowledge, so if at all possible please keep the terms as simple as possible, and I’ll try to look them up :slight_smile:

Thank you for your time.

You probably want an object to represent a question (with answers). Then have a collection of objects to store all your questions, e.g. an array or list.

A very simple example:

class Question
{
    public string text;
    public string correctAnswer;
    public string wrongAnswer1;
    public string wrongAnswer2;
}

List<Question> questions = new List<Question>();

You might want to get a bit more advanced and store the answers in a collection so that you have a varied number of answers if needed. Perhaps the correct answer is always first.

Obviously you need a way of creating each question with the appropriate strings.

If I’m understanding you correctly I might be on the right track :slight_smile:

Up until now I’ve created deactivated gameobjects called “Trivia 1”, “Trivia 2”, etc…
Within each of the I’ve added a text called “Question” located in the question display, and 3 texts called Answer 1, 2 and 3.

Could i create and array/list with the trivia objects, and just activate them randomly one set at a time?
Is that a viable approach?

What you would typically do is have single UI elements for the question and 3 answers, and simply update the text of each to reflect the current data object.

How can you create a single UI element (by that I assume you mean a “text” for instance), while ensuring that the question and answers are located correctly?

Not sure you understood me correctly. I meant a Text component for the question, then 3 Text components for the answers (on buttons). Then simply set the text field of each Text component as appropriate from your data.

Ah I think I got you now :slight_smile:
And in regards of the which answer is the correct one, it cannot be the same every time :stuck_out_tongue:
Could a solution be to add a bolean to each of the answers, so that when creating the questions, I’d be able to switch up which one is the correct one?

It doesn’t matter how the answers are given in your data. You draw your UI however you want, e.g. randomise the buttons. Just keep track of which buttons are which.

Did you know that there is an official beginner tutorial on how to make a quiz game? :slight_smile:

1 Like

No, somehow I had not found that one, while searching for answers :stuck_out_tongue:
There are some relatively big differences between his project and the one I’m trying to make, but I’ll try and complete the tutorial anyway, since there without doubt are some lessons in it, that I can use, so thanks :slight_smile:

I’m going to try and do the tutorial linked, to see how many of my questions it can solve, but thanks for the help mate!
I’ll probably be back with a few questions afterwards :slight_smile:

You should take a more generic approach.
Create your template GameObject with sub game objects with Text UI for question and your 3 answers. Save this game object as a prefab and then remove it from your scene.

Then create a kind of question manager script that will instantiate your prefab at some position and will dynamically setup text value for questin and answers. It is your QuestionManager that will get randomized question from a local storage (directly from your script or from a URL for example).

I hope this hint will help you.

Game Rules
Unified Visual Scripting Asset with rules engine