Unity freeze on certain condition.
So I am making a quiz game and I'm very new at this.
So I create a lot of question for a level. And the question will appear randomly 1 by 1 for player to answer. So to complete the level, the player must answer a certain amount of question correctly (if they answer the question wrong, they will go to game over screen instead and showing their current score). But the problem is every time I set the limit of question that needed to be answered correctly on that level, the game and whole unity engine get freeze at the 1 question before the max question. For example I set the question Index (0, 20) when I play the game it will freeze after I answer the 19th question and if I make it (0, 5) it will freeze after I answer the 4th question.
I'm so confuse. Can anybody help me? Here is the script I make.
QuestionData Script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class QuestionsData : MonoBehaviour
{
public Questions questions;
[SerializeField] private Text _questionText;
public GameObject completeLevelUI;
void Start()
{
AskQuestion();
}
public void AskQuestion()
{
var randomIndex = 0;
do
{
randomIndex = UnityEngine.Random.Range(0, 5);
}
while (questions.questionsList[randomIndex].questioned == true);
questions.currentQuestion = randomIndex;
questions.questionsList[questions.currentQuestion].questioned = true;
_questionText.text = questions.questionsList[questions.currentQuestion].question;
if (randomIndex == 5)
{
CompleteLevel();
}
}
public void ClearQuestion()
{
foreach (var question in questions.questionsList)
{
question.questioned = false;
}
}
public void CompleteLevel()
{
completeLevelUI.SetActive(true);
}
}
Congratulation Script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Congratulation : MonoBehaviour
{
public QuestionsData questionData;
void onTriggerEnter()
{
questionData.CompleteLevel();
}
}