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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can I make a list like this?

Discussion in 'Scripting' started by zigglr, Jul 6, 2018.

  1. zigglr

    zigglr

    Joined:
    Sep 28, 2015
    Posts:
    82
    I am working on a quiz game and would like a list something like this:

    Code (CSharp):
    1. questionList = new List<string>()
    2. {
    3.  
    4. question: "What color is a banana?",
    5. choices: {
    6. choice1: "blue",
    7. choice2: "red",
    8. choice3: "orange",
    9. choice4: "yellow"
    10. },
    11. answer: "yellow"
    12.  
    13. },
    14.  
    15. {
    16.  
    17. question: "What is the capital of Portugal?",
    18. choices: {
    19. choice1: "Frankfurt",
    20. choice2: "New York",
    21. choice3: "Tokyo",
    22. choice4: "Lisbon"
    23. },
    24. answer: "Lisbon"
    25.  
    26. };
    Thanks for any help. I'm using C#.
     
  2. victorML

    victorML

    Joined:
    Jul 5, 2017
    Posts:
    38
    You can simply make a List where:
    The first element could be the question
    The last element could be the answer

    Between first and last, the choices so you can print all the elements of the list except the last and compare the choice of the player with the last element of the list.

    You can look at this Unity Tutorial to learn more about lists on unity:
    Unity List Tutorial
     
  3. zigglr

    zigglr

    Joined:
    Sep 28, 2015
    Posts:
    82
    From everything i've read, I can only find resources that talk about lists like this:

    Code (CSharp):
    1. newList = new List<string>()
    2. {
    3. "firstitem",
    4. "seconditem",
    5. "thirditem"
    6. };
    But as you can see, my desired list is much more complicated than this. I'm not sure my list would even be possible in c# unity?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    I would probably do this as json with a class and do a list of that class. Which is very possible in c#. Or, if you plan to hardcode it, a list of the class with hardcoded values is just fine also.

    However, what was meant is you could simply have a string list where the first string is the question and the last string is the answer. Everything in between is possible answers.
     
  5. victorML

    victorML

    Joined:
    Jul 5, 2017
    Posts:
    38
    I'm suposing that you what something similar to a Json structure ?
    JSON Unity
    Here you have some functions to implement what you want.
    You could create a class with the parameters you want and then transform it to a JSON.

    If you are using something like GameSparks, managing the data in JSONS would be really useful to you... Otherwise, this problem could be implemented easily with a simple list...
     
    Last edited: Jul 6, 2018
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,148
    Lists can hold custom classes.

    Class:
    Code (csharp):
    1. public class QuizEntry {
    2.     public string question;
    3.     public string[] choices;
    4.     public int answer;
    5.  
    6.     public QuizEntry() { choices = new string[3]; }
    7. }
    Example:
    Code (csharp):
    1. List<QuizEntry> questions = new List<QuizEntry>();
    2.  
    3. QuizEntry qe = new QuizEntry();
    4. qe.question = "What color is a banana?";
    5. qe.choice[0] = "blue";
    6. qe.choice[1] = "red";
    7. qe.choice[2] = "orange";
    8. qe.choice[3] = "yellow";
    9. qe.answer = 3;
    10.  
    11. questions.Add(qe);