My question are all ways wrong

Hello, I am on lesson 63 and I am having trouble with my code. when every I interact with my game
it always replies with me getting the wrong answer. I believe that there is something wrong with the code but I don’t know.
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Timer : MonoBehaviour

{

[SerializeField] float timeToCompleteQuestion = 30f;

[SerializeField]float timeToShowCorrectAnswer =10f;

public bool loadNextQuestion;

public float fillFraction;

public bool isAnsweringQuestion;

float timerValue;

void Update()

{

    UpdateTimer();

}

public void CancelTimer()

{

    timerValue = 0;  

}

void UpdateTimer()

{

    timerValue -= Time.deltaTime;

    if(isAnsweringQuestion)

    {

        if(timerValue > 0)

        {

            fillFraction =timerValue/timeToCompleteQuestion;

        }

        else

        {

            isAnsweringQuestion =false;

            timerValue = timeToShowCorrectAnswer;

        }

    }

    else

    {

        if(timerValue > 0)

        {

            fillFraction =timerValue / timeToShowCorrectAnswer;

        }

        else

        {

            isAnsweringQuestion = true;

            timerValue = timeToCompleteQuestion;

            loadNextQuestion = true;

        }

    }

}

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using TMPro;

using UnityEngine.UI;

public class quiz : MonoBehaviour

{

[Header("Questions")]

[SerializeField] TextMeshProUGUI questionText;

[SerializeField] List<QuestionSO> questions= new List<QuestionSO>();

QuestionSO currentQuestion;

[Header("Answers")]

[SerializeField] GameObject[] answerButtons;

int correctAnswerIndext;

bool hasAnsweredEurly;

[Header("Button Colors")]

[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.loadNextQuestion)

   {

       hasAnsweredEurly = false;

       GetNextQuestion();

       timer.loadNextQuestion = false;

   }

   else if(!hasAnsweredEurly && timer.isAnsweringQuestion)

   {

       DisplayAnswer(-1);

       SetButtonState(false);

   }

}

public void OnAnswerSelected(int index)

{

    hasAnsweredEurly = true;

    DisplayAnswer(index);

    SetButtonState(false);

    timer.CancelTimer();

}

void DisplayAnswer(int index)

{

     Image buttonImage;

    if (index == currentQuestion.GetCorrectAnswerIndex())

    {

        questionText.text = "Correct!";

        buttonImage = answerButtons[index].GetComponent<Image>();

        buttonImage.sprite = correctAnswerSprite;

    }

    else

    {

       correctAnswerIndext = currentQuestion.GetCorrectAnswerIndex();

       string correctAnswer =currentQuestion.Getanswers(correctAnswerIndext);

       questionText.text ="Sorry, the correct answer was;\n" +correctAnswer;

       buttonImage =answerButtons[correctAnswerIndext].GetComponent<Image>();

       buttonImage.sprite = correctAnswerSprite;

    }

}

 void GetNextQuestion()

{

    if(questions.Count>0)

    {

    SetButtonState(true);

    SetDefaultButtonSprites();

    GetRandomQuestion();

    DisplayQuestion();

    }

}

void GetRandomQuestion()

{

    int index =Random.Range(0,questions.Count);

    currentQuestion =questions[index];

    if(questions.Contains(currentQuestion))

    {

        questions.Remove(currentQuestion);

    }

   

}

void DisplayQuestion()

{

    questionText.text =currentQuestion . GetQuestion();

    for(int i=0; i < answerButtons.Length; i++)

    {

        TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();

        buttonText.text = currentQuestion.Getanswers(i);

    }

}



void SetButtonState(bool state)

{

    {   for(int i=0; i<answerButtons.Length; i++)

        {

            Button button =answerButtons[i].GetComponent<Button>();

            button.interactable = state;

        }

    }

}

void SetDefaultButtonSprites()

{

    for(int i = 0; i < answerButtons.Length; i++)

    {

        Image buttonImage = answerButtons[i].GetComponent<Image>();

        buttonImage.sprite = defaultAnswerSprite;

    }

}

}

Hi Owen,

Are there any error messages in your console during runtime? Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? And have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe the assigned values in the OnClick fields of your Buttons do not match the respective index of the expected question.


See also:

Hello I am now getting this error CS0161
Assets\Scripets\Quiz.cs(17,54): error CS1061: ‘GameObject’ does not contain a definition for ‘GetComponetInChildren’ and no accessible extension method ‘GetComponetInChildren’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive or an assembly reference?)

GetComponetInChildren should be a command right?
as well I decided to start over and I may have disable some things in VS.

Have you already taken a veeeeery close look at the spelling of the method the compiler is complaining about?

If I get an error message stating that “‘Classname’ does not contain a definition for ‘Something’”, the first thing I do is to check the spelling of “Something”. And if the spelling is correct, I check if the “something” is public (accessible from outside the object).

In most cases, this helps me find the issue.

1 Like

So I decided to start from scratch again and I got it working.

Starting from scratch is a good way to reinforce what you learnt. I’m glad that you also managed to fix the problem. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.