List...Multiple Strings...How?!

Hey all,

Been trying to do this for ages and after much research and trying to find a way of doing it. Basically, I’m making a quiz game, sounds simple enough? I thought it was…

So, my question is, how do I generate a random question, with one correct answer to that question and 3 wrong answers relating to that question. I did have my own way, which was having 3 very long lists and constantly doing .Add(“No, you’re wrong”) or whatever. But to me, that sounds like an awful way of doing things. I’ve tried using Arrays but found out they are only available in Java, and I prefer C# so I ended up trying to find a way to do it.

Now, through the research, I found that someone said “Create a string array that holds multiple strings and always have the correct answer as the first element” so that’s what I tried setting out to do…

I’ve tried creating a class to hold all data of 3 different string values, didn’t work.

I’ve tried .AddRage, worked, but ended up saying there were 6 elements in the list when there should have been only two (That’s what I wanted it to say) when I did it like this…

stringList.InsertRange (stringList.Count, new string[]{"One", "Two", "Three"});
        stringList.InsertRange (stringList.Count, new string[]{"Four", "Five", "Six"});

So, to put it simply, is there an easy way of creating a quiz game with one question, four choices, and three being wrong?

From your description of things you’ve tried, it sounds like you have probably misunderstood a lot of basic language information, and you might want to take a break to study some fundamentals. For example:

Arrays do exist in C#. In fact, the code example you posted of what you’re currently trying uses arrays (“string[ ]” means an array of strings). And Java is not one of the languages available in Unity (Java and JavaScript are completely different).

C# arrays don’t work in quite the same way as JavaScript arrays, but C# has a wide array of collection classes with various properties, so you can probably find one with the features you want (provided you can figure out which features you want).

The particular problem with your posted code seems to be that AddRange() is used to add several elements to a collection, whereas you apparently wanted to add a single element that was, itself, a nested collection. (Like an array of arrays.) I’m guessing you’re using a List (or something similar), but if you want to have a list of questions and each individual question to have a list of strings, then you need a list of lists of strings, i.e. a List<List>.

1 Like

I would actually say that each question should be an object of a custom class. Something like this:

[System.Serializable]
public class TriviaQuestion {
public string question;
public List<string> answers;
public string correctAnswer;
// and any other stuff about the question....
public int questionPointValue;
public bool isDailyDouble;
}

....

List<TriviaQuestion> allQuestions = new List<TriviaQuestion>();

Something along those lines.

1 Like

If it’s always three wrong answers and one right answer and you’re having trouble figuring out arrays and lists you could always just do this:

[System.Serializable]
public class Question {
public string question;
public string correctAnswer;
public string wrongAnswer1;
public string wrongAnswer2;
public string wrongAnswer3;
}

then add a monobehaviour like

public class AllTheQuestions : MonoBehaviour {
  public List<Question> allQuestions;
}

Put it on a game object, and then in the inspector you can fill out all the questions and answers.

2 Likes

Thanks guys :smile:

I’ve tried that method before, I can’t remember what the result I was, think I was just getting errors. I probably did something wrong though haha. I will try it again! :smile: Will let you know how I get on :slight_smile:

Update: Tried what was suggested, it works! Only problem now is, getting randomly placed correct and wrong answers in my OnGUI() but that shouldn’t be too much of a problem, I’ll just change their positions on screen xD