Whenever I start up my game it shows the answer when I haven’t even clicked anything for some reason, I can’t figure out how, I’ve watched and rewatched the video over and over again, but I still can’t find the answer.
Can you post some screenshots of what is going? Try to include pics of the problem, and also the Hierarchy and Inspector so we can have a better idea of how to help you.
Also paste your code in here so we can read your code.
Hi MasterCheese,
Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?
The image of my problem is below.
What happens here is that I don’t click anything and the correct answer shows.
I can’t see any difference between my code and Gary’s code, I checked the lectures project changes but I still can’t find anything.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class Quiz : MonoBehaviour
{
[Header("Questions")]
[SerializeField]TextMeshProUGUI questionText;
[SerializeField] QuestionSO question;
[Header("Answers")]
[SerializeField] GameObject[] answerButtons;
int correctAnswerIndex;
bool hasAnsweredEarly;
[Header("Button Colors")]
[SerializeField] Sprite defaultAnswerSprite;
[SerializeField] Sprite correctAnswerSprite;
[Header("Timer")]
[SerializeField] Image timerImage;
Timer timer;
void Start()
{
timer = FindObjectOfType<Timer>();
GetNextQuestion();
//DisplayQuestion();
}
void Update()
{
timerImage.fillAmount = timer.fillFraction;
if(timer.loadNextQuestion)
{
hasAnsweredEarly = false;
GetNextQuestion();
timer.loadNextQuestion = false;
}
else if(!hasAnsweredEarly && !timer.isAnsweringQuestion)
{
DisplayAnswer(-1);
SetButtonState(false);
}
}
public void OnAnswerSelected(int index)
{
hasAnsweredEarly = true;
DisplayAnswer(index);
SetButtonState(false);
timer.CancelTimer();
}
void DisplayAnswer(int index)
{
Image buttonImage;
if(index == question.GetCorrectAnswerIndex())
{
questionText.text = "Correct! The clone troopers were cloned off of jango fett the mandalorian.";
buttonImage = answerButtons[index].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
else
{
correctAnswerIndex = question.GetCorrectAnswerIndex();
string correctAnswer = question.GetAnswer(correctAnswerIndex);
questionText.text = "Sorry, the correct answer was jango fett, boba is jango's son, kal skirita is a clone trainer, and the Mandalorian was just a young child at the time.";
buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
}
void GetNextQuestion()
{
SetButtonState(true);
SetDefaultButtonSprites();
DisplayQuestion();
}
void DisplayQuestion()
{
questionText.text = question.GetQuestion();
for(int i = 0; i < answerButtons.Length; i++)
{
TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
buttonText.text = question.GetAnswer(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;
}
}
}
When does the correct answer appear? After a few seconds? Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Is it always the same answer that gets highlighted? And what happens if you click on it? Does the next question get loaded and the/an answer gets selected again even though you did not click anything?
The correct answer appears immediately, The Debug.Logs Gary added show it doing the countdown to answer the question, and when I click on the correct answer shows the correct answer. So, I think it is something wrong the the sprite rather than the question.
Gary’s idea is that the answer gets highlighted when DisplayAnswer gets called. Add a Debug.Log to that method to see if it causes your problem. Maybe DisplayAnswer gets called when the game starts. In that case, the problem is very likely that DisplayAnswer got called.
You could also add a Debug.Log to OnAnswerSelected to see if something triggers that method even though you did not click a button.
Another potential culprit could be GetNextQuestion, which is called in Update. Add Debug.Logs to Update and GetNextQuestion.
What value does timer.loadNextQuestion
have? Maybe the problem is not in your Quiz class but in the Timer class, so check that one, too.
Try to narrow down the problem by learning what happens during runtime. The code might be correct but we do not know the value of, for example, timer.loadNextQuestion
just by staring at the variable. The value gets assigned at runtime, hence we have to check the value at runtime.
When I put a debug.log on the display answer it said it didn’t display the answer. OnAnswerSelected’s debug.log says nothing meaning that nothing triggered that. Timer.loadNextQuestion’s state is false. In unity is says I’m still answering the question. I’m still looking into the problem. And how do I add a Debug.Log directly to GetNextQuestion? By the way did you look over my code in post 5? Do you see any visible errors?
Never mind about any of this I’m going to start over from the beginning. I think that way I can observe more carefully since I’ve already done it once over. Thnak you for trying to help me though. Best of luck helping other game designers.
-Mastercheese.
You’re welcome. Sometimes, starting over is better than looking for a problem “forever”. It’ll also help you gain a better understanding of what is going on.
Maybe removing the timer code is sufficient. You probably won’t have to recreate your entire game project unless you want to do that.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.