Noob here
I was wondering what recommendations you would have about a good way to store a sizable amount of questions (and answers) for a multiple-choice type game.
If it were just 10-15 questions I might hard code them, but if I want to have many more, I imagine there’s a better solution.
Maybe in to an array?
Could I create some type class to define each question/answer?
Here's a very simple example of how you would create a class named Question.
It demonstrates how you would use a for loop to initialize the array full of dummy values.
It then tests the data by printing a bunch of labels with a for loop.
I'm sure there are errors... I just typed it up real quick.
If there are no errors, you should be able to use this script as is.
A more practical approach would be to get rid of the Start function (it's only for an example) and define your questions in the Inspector.
If you add that script to a GameObject, you should see a slot in the Inspector named Array Of Questions. You can populate this by entering a size then typing text into the slots named Question Text and Answer Text for each element in the array.
Hope this helps.
var arrayOfQuestions : Question [];
class Question
{
var questionText : String;
var answerText : String;
/* example constructor (creates a Question from two params) */
function Question ( q : String, a : String )
{
questionText = q;
answerText = a;
}
}
/* initialize the question array in the start function */
function Start ()
{
arrayOfQuestions = new Question [20];
/* use a for loop to create dummy questions */
for ( var i = 0; i < 20; i ++ ) {
var someQ : String = "Question number: " + i;
var someA : String = "Answer number: " + i;
/* use the example constructor from above */
var freshQuestion : Question = new Question(someQ, someA);
arrayOfQuestions *= freshQuestion;*
*}*
*}*
_/* test it */_
*function OnGUI ()*
*{*
*var labelRect : Rect = Rect(200, 200, 300, 30);*
*for ( var thisQ : Question in arrayOfQuestions ) {*
*var txt = "Q: " + thisQ.questionText + " A: " + thisQ.answerText;*
*GUI.Label(labelRect, txt);*
*labelRect.y += labelRect.height;*
*}*
*}*
*```*