Quiz Master: On starting the game, 2 questions are deleted instead of 1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class Quiz : MonoBehaviour
{

    [Header("Questions")]
    [SerializeField] TextMeshProUGUI quesText;
    [SerializeField]List<Question> questions = new List<Question>();
    Question currentQuestion;

    [Header("Answers")]
    [SerializeField] GameObject[] ansButtons;
    int correctAnswerIndex;
    bool hasAnsEarly;

    [Header("Buttons")]
    [SerializeField] Sprite defaultAnswerSprite;
    [SerializeField] Sprite correctAnswerSprite;

    [Header("Timer")]
    [SerializeField] Image timerImage;
    Timer timer;

    void Start()
    { 
        timer = FindObjectOfType<Timer>();
    
    }

    void Update()
    {
        timerImage.fillAmount = timer.fillFraction;
        if (timer.loadNextQues)
        {
            hasAnsEarly = false;
            GetNextQues();
            timer.loadNextQues = false;
        }
        else if (!hasAnsEarly && !timer.isAnswering)
        {
            DisplayAnswer(-1);
            ButtonState(false);
        }
    }

    public void OnAnswerSelected(int index)
    {
        hasAnsEarly = true;
        DisplayAnswer(index);
        ButtonState(false);
        timer.CancelTimer();
    }

    void DisplayAnswer(int index)
    {
        Image buttonImage;
        if(index == currentQuestion.GetCorrectAnswerIndex())
        {
            quesText.text = "Correct!";
            buttonImage = ansButtons[index].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
        }
        else if(index == -1)
        {
            quesText.text = "Correct Answer is " + currentQuestion.GetAnswer(currentQuestion.GetCorrectAnswerIndex());
            buttonImage = ansButtons[currentQuestion.GetCorrectAnswerIndex()].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite; 
        }
        else{
            quesText.text = "Wrong answer!\nCorrect Answer is " + currentQuestion.GetAnswer(currentQuestion.GetCorrectAnswerIndex());
            buttonImage = ansButtons[currentQuestion.GetCorrectAnswerIndex()].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
        }
    }

    void GetNextQues()
    {
        if(questions.Count > 0)
        {
            ButtonState(true);
            SetDefaultButtonSprites();
            GetCurrentQuestion();
            DisplayQuestion();
        }
       
    }

    void GetCurrentQuestion()
    {
        int qIndex = Random.Range(0, questions.Count);
        currentQuestion = questions[qIndex];
        Debug.Log("Current qestion"+ currentQuestion);
        if (questions.Contains(currentQuestion))
        {
            questions.Remove(currentQuestion);
        }
    }

    void DisplayQuestion()
    {
        quesText.text = currentQuestion.GetQuestion();


        for(int i = 0; i < ansButtons.Length; i++)
        {
            TextMeshProUGUI buttonText = ansButtons[i].GetComponentInChildren<TextMeshProUGUI>();

            buttonText.text = currentQuestion.GetAnswer(i);
        }
    }

    void ButtonState(bool state)
    {
        for (int i = 0; i< ansButtons.Length; i++ )
        {
            Button button = ansButtons[i].GetComponent<Button>();
            button.interactable = state;
        }
    }

    void SetDefaultButtonSprites()
    {
        for (int i = 0; i< ansButtons.Length; i++ )
        {
            Image buttonImage = ansButtons[i].GetComponent<Image>();
            buttonImage.sprite = defaultAnswerSprite;
        }
    }

}

This is the Quiz script and its almost same as the one that Gary used. When I start the game only one question, the question that was displayed, was supposed to be deleted. But instead 2 questions are being deleted. This is after removing the GetNextQues() method call, where only 1 question was to deleted. When I called the method in Start(), 3 questionsare being removed. Can anyone help me with it? Copying Gary’s script didn’t help either.

1 Like

have the same issue…
i even tried coping Garys script from the Git and it still start from the second question…

The issue may not be in Quiz but in the Timer

I am seeing the same issue. No matter what I do, it is deleting two random questions when it first starts up. I also copied the code from Git for both the Quiz and Timer but two questions are getting deleted. I added some Log statements to call out the methods as they are being called. On loading, it goes through two questions.

Go to the Timer Gameobject and make sure that “load next question” in the inspector is unchecked

1 Like

Unchecking “load next question” in the inspector resolved the problem of 2 questions getting deleted for me, but created another issue.

Now, no questions are loaded and I get:
NullReferenceException: Object reference not set to an instance of an object
Quiz.DisplayAnswer (System.Int32 index) (at Assets/Scripts/Quiz.cs:61)
Quiz.Update () (at Assets/Scripts/Quiz.cs:44)

After some frustration with the lectures starting at the timer lecture, I have just copied and pasted the code directly from the git scripts and this issue is still happening. I am guessing it is happening somewhere in Unity and not Visual Studio, but cannot figure out what is going on.

Any suggestions or thoughts would be appreciated.

Figured it out. I remembered that there were a lot of other posts about a null reference exception. This post fixed my issue:

https://community.gamedev.tv/t/nullreferenceexception/218345?_gl=18bruk2_gaMTkxMjYzMzI5LjE2ODI1NDUxMjE._ga_2C81L26GR9*MTY4MzY1NDgyOS4yOC4xLjE2ODM2NTUwMzYuMC4wLjA.

Apparently there is a bug in the code that is not fixed until a later lecture. Adding a simple extra conditional to the Update method in Quiz.cs will fix it.

With all due respect, I don’t know why there is no update in the “Lists” video to explain this issue. It’s been going on for over a year. I’ll echo what others have said that this whole section seems to be a bit of a mess.

For me, I had no issues until the timer lecture. After that, it’s been a struggle. I’m no coding expert, but I have been working professionally in R as a researcher for 5 years and am still having difficulties with the last few lectures. I can only imagine how frustrating it would be for someone with zero coding experience.

Still think overall the course is great and both lecturers are good, don’t regret purchasing it or anything like that.

But it’s clear this section needs another refresher. Some of these sections need to be broken in 2 smaller videos, and include some more emphasis on troubleshooting and talking about common errors. That was discussed in the Driver and Snowboarder sections, but hasn’t been touched on here at all.

OMFG, I was troubleshooting this for like an hour, installed the VSCode Unity tools so i could set up the debugger and all, and I didn’t ever think to check this. Either way, i can use break points in VSCode, now, but dang if i’m not frustrated about that, haha.

Privacy & Terms