Hi, i have made a quiz (from a course). I have no syntax errors.
But when i start. It should read stuff from a scriptable object and then destroy that one after reading.
But when i start up it automatically deletes 2 from the list.
I cant seem to figure it out.
Picture 1 with the 5 elements in the inspector.
Picture 2 with only 3 right after start-up.
Thanks for the help!
picture 1
picture 2
TIMER CLASS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Timer : MonoBehaviour
{
[SerializeField]float timeToAnswer = 30f;
[SerializeField]float timeToShowCorrectAnswer = 10f;
public bool loadNextQuestion = true;
public bool isAnsweringQuestion = true;
public float fillFraction;
float timerValue;
void Update()
{
UpdateTimer();
}
public void CancelTimer()
{
timerValue = 0;
}
void UpdateTimer()
{
timerValue -= Time.deltaTime;
if(isAnsweringQuestion)
{
if(timerValue > 0)
{
fillFraction = timerValue / timeToAnswer;
Debug.Log("Test1");
}
else
{
timerValue = timeToShowCorrectAnswer;
isAnsweringQuestion = false;
Debug.Log("Test 2");
}
}
else //isAnsweringQuestion == false
{
if(timerValue > 0)
{
fillFraction = timerValue / timeToShowCorrectAnswer;
Debug.Log("Test 3");
}
else
{
timerValue = timeToAnswer;
Debug.Log($"testTimer value set: {timerValue}");
isAnsweringQuestion = true;
Debug.Log($"test{isAnsweringQuestion}");
loadNextQuestion = true;
Debug.Log($"test{loadNextQuestion}");
Debug.Log("Test 4");
}
}
//Debug.Log($"{isAnsweringQuestion} : {timerValue}");
}
}
QUIZ CLASS
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;
[Header("Button colors")]
[SerializeField]Sprite defaultAnswerSprite;
[SerializeField]Sprite correctAnswerSprite;
[Header("Timer")]
[SerializeField]Image timerImage;
Timer timer;
bool hasAnsweredEarly;
void Start()
{
timer = FindObjectOfType<Timer>();
}
void Update()
{
timerImage.fillAmount = timer.fillFraction;
if(timer.loadNextQuestion == true)
{
hasAnsweredEarly = false;
GetNextQuestion();
timer.loadNextQuestion = false;
}
else if(!hasAnsweredEarly && !timer.isAnsweringQuestion)
{
DisplayAnswer(-1);
SetButtonState(false);
}
}
void DisplayAnswer(int index)
{
if(index == currentQuestion.GetCorrectIndex())
{
questionText.text = "Correct!";
Image buttonImage = answerButtons[index].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
else
{
questionText.text = $"FOUT! \n{currentQuestion.GetAnswer(currentQuestion.GetCorrectIndex())}\n was het goede antwoord!";
Image buttonImage = answerButtons[currentQuestion.GetCorrectIndex()].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
}
}
public void OnAnswerSelected(int index)
{
hasAnsweredEarly = true;
DisplayAnswer(index);
SetButtonState(false);
timer.CancelTimer();
}
void GetNextQuestion()
{
SetButtonState(true);
SetDefaultButtonSprites();
GetRandomQuestion();
DisplayQuestion();
}
void GetRandomQuestion()
{
int index = Random.Range(0, questions.Count);
Debug.Log($"current index{index}");
Debug.Log($"questions count:{questions.Count}");
currentQuestion = questions[index];
if(questions.Contains(currentQuestion))
{
Debug.Log("Contains question");
questions.Remove(currentQuestion);
Debug.Log("Delete question");
}
}
public void DisplayQuestion()
{
questionText.text = currentQuestion.GetQuestion();
for(int i = 0; i < answerButtons.Length; i++)
{
TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
buttonText.text = currentQuestion.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;
}
}
}