Break the loop when there are no more questions
Hello, I've been stuck on this for a couple days now, I can't seem to break out of the questions I have, after 20 questions have ended, I would like to display a score scene for example. I have tried adding a basic if statement to test after answer 3 out of 20 questions but my problem is that when I answer 2 questions and decide to go back to the main menu and hit "play" again it would continue with the 2 questions answered. if (unansweredQuestions.Count == 17) { timeBetweenQuestions = 1f; yield return new WaitForSeconds(timeBetweenQuestions); SceneManager.LoadScene("ScorePage"); Debug.Log("No more questions. Back to score menu"); }
Maybe it's better to break out upong reaching a certain score? I hope I explained my problem well, any help would be greatly appreciated.
This is my GameManager code
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public Question[] questions;
private static List<Question> unansweredQuestions;
private Question currentQuestion;
[SerializeField]
private Text factText;
[SerializeField] private float timeBetweenQuestions = 1f;
[SerializeField]
private Text trueAnswserText;
[SerializeField]
private Text falseAnswerText;
[SerializeField]
private Animator animator;
void Start()
{
if (unansweredQuestions == null || unansweredQuestions.Count == 0)
{
unansweredQuestions = questions.ToList<Question>();
}
SetCurrentQuestion();
}
public void Mute()
{
AudioListener.pause = !AudioListener.pause;
}
void SetCurrentQuestion()
{
int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
currentQuestion = unansweredQuestions[randomQuestionIndex];
factText.text = currentQuestion.fact;
if (currentQuestion.isTrue)
{
trueAnswserText.text = "CORRECT";
falseAnswerText.text = "WRONG";
}
else
{
trueAnswserText.text = "WRONG";
falseAnswerText.text = "CORRECT";
}
}
IEnumerator TransitionToNextQuestion()
{
unansweredQuestions.Remove(currentQuestion);
if (unansweredQuestions.Count == 17) // all questions answered
{
timeBetweenQuestions = 2f;
yield return new WaitForSeconds(timeBetweenQuestions);
SceneManager.LoadScene("ScorePage");
Debug.Log("No more questions. Back to score menu");
}
yield return new WaitForSeconds(timeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void UserSelectTrue()
{
animator.SetTrigger("True");
if (currentQuestion.isTrue)
{
Debug.Log("CORRECT");
Score.scoreValue += 1;
}
else
{
Debug.Log("FALSE");
}
StartCoroutine(TransitionToNextQuestion());
}
public void UserSelectFalse()
{
animator.SetTrigger("False");
if (!currentQuestion.isTrue)
{
Debug.Log("CORRECT");
}
else
{
Debug.Log("FALSE");
}
StartCoroutine(TransitionToNextQuestion());
}
public void UserSelectExit()
{
Debug.Log("Application has quit.");
}
}