Hello there,
i have the problem since i’ve integrated the timer into my code, before that it worked normally but not anymore. The Buttons are still interactable but they dont load from the Text.
Here is all the Code, the debuglog and a images showcasing it.
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;
// The Color Changing After Click Variables
[Header("Buttons")]
[SerializeField] Sprite defaultAnswerSprite;
[SerializeField] Sprite correctAnswerSprite;
[SerializeField] Sprite falseAnswerSprite;
// The Timer
[Header("Timer")]
[SerializeField] Image timerImage;
Timer timer;
void start()
{
timer = FindObjectOfType<Timer>();
displayQuestion();
}
void update()
{
timerImage.fillAmount = timer.fillFraction;
if(timer.loadNextQuestion)
{
getNextQuestion();
timer.loadNextQuestion = false;
}
}
public QuestionSO getQuizQuestion()
{
return question;
}
public void onAnswerSelected(int index)
{
Image buttonImage;
if(index == question.getCorrectAnswerIndex())
{
questionText.text = "Correct!";
buttonImage = answerButtons[index].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
else
{
correctAnswerIndex = question.getCorrectAnswerIndex();
string correctAnswer = question.getAnswer(correctAnswerIndex);
buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>(); // Hier wird der Index der richtigen Antwort genutzt
buttonImage.sprite = correctAnswerSprite;
buttonImage = answerButtons[index].GetComponent<Image>(); // und hier der Index des Feldes, dass wir angeclickt haben
buttonImage.sprite = falseAnswerSprite;
questionText.text = "WRONG! \nit's " + correctAnswer;
}
setButtonState(false);
timer.cancelTimer();
}
private void getNextQuestion()
{
setButtonState(true);
displayQuestion();
setDefaultButtonSprite();
}
private void setDefaultButtonSprite()
{
Image buttonImage;
for(int i = 0; i < answerButtons.Length; i++)
{
buttonImage = answerButtons[i].GetComponent<Image>();
buttonImage.sprite = defaultAnswerSprite;
}
}
private 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);
}
}
private void setButtonState(bool state)
{
for(int i = 0; i < answerButtons.Length; i++)
{
Button button = answerButtons[i].GetComponent<Button>();
button.interactable = state;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Timer : MonoBehaviour
{
[SerializeField] float timeToCompleteQuestion = 30f;
[SerializeField] float timeToShowCorrectAnswer = 30f;
public bool loadNextQuestion;
public float fillFraction;
bool isAnsweringQuestion;
float timerValue;
void update()
{
updateTimer();
}
public void cancelTimer()
{
timerValue = 0;
}
private 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;
[CreateAssetMenu(menuName = "Quiz Question", fileName = "New Question")]
public class QuestionSO : ScriptableObject
{
[TextArea(2,6)]
[SerializeField] string question = "Enter new question here";
[SerializeField] string[] answers = new string[4];
[SerializeField] int correctAnswerIndex;
public string getQuestion()
{
return question;
}
public int getCorrectAnswerIndex()
{
return correctAnswerIndex;
}
public string getAnswer(int index)
{
return answers[index];
}
}