Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to make a better quiz game?

Discussion in 'Scripting' started by aiyan21, Mar 1, 2023.

  1. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    Hello, we have a game development project for school, and time's running out.
    we didn't have any game development lessons so I've been trying to learn as I'm creating the game.

    I used Brackey's Quiz Tutorial (and tried other tutorials but to no avail because they're all the same) but it kept restarting the quiz (and the score) and the quiz system didn't really fit my game (it's more of a personality test/quiz rather than a trivia quiz. (I've tried applying visual novel properties because it fits the description more but my knowledge about game dev is too bad for me to construct my own codes)

    Idk what to do anymore :""") pls help me.
    heres the codes from brackey's quiz tutorial:
    Question.cs:
    Code (CSharp):
    1. [System.Serializable]
    2. public class Question
    3. {
    4.     [TextArea(4, 10)]
    5.     public string question;
    6.     public bool IsIntrovert;
    7.     public bool IsExtrovert;
    8. }
    GameManager.cs (with some of the codes that I added trying to adapt my game into it:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5. using UnityEngine.UI;
    6. using TMPro;
    7. using UnityEngine.SceneManagement;
    8.  
    9. public class GameManager : MonoBehaviour
    10. {
    11.     public Question[] questions;
    12.     private static List<Question> unansweredQuestions;
    13.  
    14.     private Question currentQuestion;
    15.     private float extrovertscore;
    16.     private float introvertscore;
    17.     private float finalEscore;
    18.     private float finalIscore;
    19.     private string EIresult;
    20.    
    21.  
    22.     [SerializeField]
    23.     private TextMeshProUGUI questionText;
    24.  
    25.     [SerializeField]
    26.     private float timeBetweenQuestions = 1f;
    27.  
    28.         void Start()
    29.         {
    30.             if (unansweredQuestions == null || unansweredQuestions.Count == 0)
    31.             {
    32.                 unansweredQuestions = questions.ToList<Question>();
    33.             }
    34.  
    35.             SetCurrentQuestion();
    36.             //Debug.Log(currentQuestion.question + " is " + currentQuestion.IsIntrovert);
    37.         }
    38.         void SetCurrentQuestion()
    39.         {
    40.             int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
    41.             currentQuestion = unansweredQuestions[randomQuestionIndex];
    42.  
    43.             questionText.text = currentQuestion.question;
    44.  
    45.             unansweredQuestions.RemoveAt(randomQuestionIndex);
    46.         }
    47.  
    48.         IEnumerator TransitionToNextQuestion()
    49.         {
    50.             unansweredQuestions.Remove(currentQuestion);
    51.  
    52.             yield return new WaitForSeconds(timeBetweenQuestions);
    53.  
    54.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    55.         }
    56.  
    57.  
    58.  
    59.     ///choices
    60.     public void UserSelectAgree()
    61.     {
    62.         if (currentQuestion.IsExtrovert)
    63.         {
    64.             Debug.Log("extrovert score +10");
    65.             extrovertscore = extrovertscore + 10;
    66.             Debug.Log("extrovert score: " + extrovertscore);
    67.         }
    68.         else if (currentQuestion.IsIntrovert)
    69.         {
    70.             Debug.Log("introvert score +10");
    71.             introvertscore = introvertscore + 10;
    72.             Debug.Log("introvert score: " + introvertscore);
    73.         }
    74.         StartCoroutine(TransitionToNextQuestion());
    75.     }
    76.  
    77.     public void UserSelectDisagree()
    78.     {
    79.         if (currentQuestion.IsIntrovert)
    80.         {
    81.             Debug.Log("extrovert score +10");
    82.             extrovertscore = extrovertscore + 10;
    83.             Debug.Log("extrovert score: " + extrovertscore);
    84.         }
    85.         else if (currentQuestion.IsExtrovert)
    86.         {
    87.             Debug.Log("introvert score +10");
    88.             introvertscore = introvertscore + 10;
    89.             Debug.Log("introvert score: " + introvertscore);
    90.         }
    91.         StartCoroutine(TransitionToNextQuestion());
    92.     }
    93.        
    94.     public void UserSelectNeutral()
    95.     {
    96.         if (currentQuestion.IsIntrovert)
    97.         {
    98.             Debug.Log("extrovert score and introvert score +5");
    99.             extrovertscore = extrovertscore + 5;
    100.             introvertscore = introvertscore + 5;
    101.             Debug.Log("extrovert score: " + extrovertscore);
    102.             Debug.Log("introvert score: " + introvertscore);
    103.         }
    104.         else if (currentQuestion.IsExtrovert)
    105.         {
    106.             Debug.Log("extrovert score and introvert score +5");
    107.             extrovertscore = extrovertscore + 5;
    108.             introvertscore = introvertscore + 5;
    109.             Debug.Log("extrovert score: " + extrovertscore);
    110.             Debug.Log("introvert score: " + introvertscore);
    111.         }
    112.         StartCoroutine(TransitionToNextQuestion());
    113.     }
    quiz ui:
    upload_2023-3-1_22-34-42.png
     
  2. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    I'm very sorry if the codes are too messy, I'm still not comfortable with unity (and i don't have much programming experience)TT TT
     
  3. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    What's the issue that you're having?
     
  4. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    The score keeps resetting and the questions are on the loop (i mean it's in the codes but idk what to replace it with cause i dont really understand anything TT)

    to describe what I'm aiming to make:
    basically a personality game which is why there are agree - neutral - disagree options.
    the options will add value to two variables (introvertscore and extrovert score) so basically the game will have like two endings.

    but with what i have now its very very impossible TT
     
  5. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    as you can see here, the value is not adding up TT, and 3 questions are on a loop (they should be played just once)
    upload_2023-3-1_23-0-1.png
     
  6. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    Lets go through it:
    - unansweredQuestions does not need to be static, and actually shouldn't be, so remove static.

    - You are trying to remove 'currentQuestion' from unansweredQuestions twice, once in SetCurrentQuestion() and again in TransitionToNextQuestion(), I'd suggest you remove the one from TransitionToNextQuestion()

    - You are loading a new scene every time you change the question, so your GameManager class will reset itself, and the questions will reset too. So just remove the scene change and replace it with SetCurrentQuestion() and it should be fine.

    Let me know if you have any questions.
     
    aiyan21 likes this.
  7. aiyan21

    aiyan21

    Joined:
    Mar 1, 2023
    Posts:
    21
    Thanks!! its working now : D

    but i have a question, is there a way to use next and previous button?
    next to confirm the answer & proceed and previous to go back to last question (and maybe change the answer)
     
  8. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    Yes, but you'll have to keep track of the questions.

    My suggestion for this is to keep your current structure, but don't ever remove values from unansweredQuestions. Instead, shuffle the unansweredQuestions list in Start, then use an int variable to keep track of an index that you can use to index the unansweredQuestions list. Like this:

    Code (CSharp):
    1.     List<Question> unansweredQuestions = new List<Question>();
    2.     private int index = 0;
    3.  
    4.     public void Start()
    5.     {
    6.         unansweredQuestions = unansweredQuestions.OrderBy(x => Guid.NewGuid()).ToList();
    7.         index = 0;
    8.         UpdateQuestion();
    9.     }
    10.  
    11.     public void NextQuestion()
    12.     {
    13.         index += 1;
    14.         // make sure to check index < unansweredQuestions.Count
    15.         UpdateQuestion();
    16.     }
    17.  
    18.     public void PreviousQuestion()
    19.     {
    20.         index -= 1;
    21.         // make sure to check index > 0
    22.         UpdateQuestion();
    23.     }
    24.  
    25.     public void UpdateQuestion()
    26.     {
    27.         Question currentQuestion = unansweredQuestions[index];
    28.         questionText.text = currentQuestion.text;
    29.     }
    But you'll need to keep track of the extrovert and introvert points somehow, probably within the questions themselves, and use another function for counting up their values. I'll leave that bit to you.
     
    Last edited: Mar 1, 2023
    aiyan21 likes this.