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. Dismiss Notice

Brackeys Quiz Game into Multiple Choice

Discussion in 'Scripting' started by Deleted User, Jun 18, 2017.

  1. Deleted User

    Deleted User

    Guest

    Hello, I'm trying to make a quiz game with a tutorial made by Brackeys (https://www.youtube.com/playlist?list=PLPV2KyIb3jR7ucA2yo5pjvKY0cJmNTq2L) but I don't know how to turn the true and false format into a multiple choice one with 4 different answers. I'm still unfamiliar to C# and I hope I can get help.

    Here's my code:
    Game Master:


    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System.Linq;
    6.  
    7. public class GameMaster : MonoBehaviour {
    8.  
    9.     public Question[] questions;
    10.     private static List<Question> unansweredQuestions;
    11.  
    12.     private Question currentQuestion;
    13.  
    14.     [SerializeField]
    15.     private Text questionText;
    16.  
    17.     [SerializeField]
    18.     private Text answer1;
    19.  
    20.     [SerializeField]
    21.     private Text answer2;
    22.  
    23.     [SerializeField]
    24.     private Text answer3;
    25.  
    26.     [SerializeField]
    27.     private Text answer4;
    28.  
    29.  
    30.     void Start()
    31.     {
    32.         if (unansweredQuestions == null || unansweredQuestions.Count == 0)
    33.         {
    34.             unansweredQuestions = questions.ToList<Question>();
    35.         }
    36.  
    37.         SetCurrentQuestion ();
    38.     }
    39.  
    40.     void SetCurrentQuestion ()
    41.     {
    42.         int randomQuestionIndex = Random.Range (0, unansweredQuestions.Count);
    43.         currentQuestion = unansweredQuestions [randomQuestionIndex];
    44.  
    45.         questionText.text = currentQuestion.question;
    46.         answer1.text = currentQuestion.answer1;
    47.         answer2.text = currentQuestion.answer2;
    48.         answer3.text = currentQuestion.answer3;
    49.         answer4.text = currentQuestion.answer4;
    50.  
    51.         unansweredQuestions.RemoveAt(randomQuestionIndex);
    52.     }
    53.     public void UserSelect ()
    54.     {
    55.         if (currentQuestion.rightAnswer) {
    56.             Debug.Log ("Correct!");
    57.         } else
    58.         {
    59.             Debug.Log ("Wrong!");
    60.         }
    61.     }
    62.        
    63. }
    64.  
    Questions:

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class Question {
    4.     public string question;
    5.     public string answer1 = "";
    6.     public string answer2 = "";
    7.     public string answer3 = "";
    8.     public string answer4 = "";
    9.     public int rightAnswer = 1;
    10. }


    Thanks.
     
    NatalieW15 likes this.
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could either make your answers classes (each contain an int or bool for 'right answer') and add the (4) answers to your question class.. or you could use an array for your answers and 'right answer' could be the index which is correct.
    A couple of options to consider :)
     
    NatalieW15 likes this.
  3. NatalieW15

    NatalieW15

    Joined:
    Jul 30, 2019
    Posts:
    16


    This should get you on the right direction.