4 lines of code makes untiy crash

These lines of code to make random.range not repeat numbers makes unity crash after playing for a while. I removed these lines of code below and it didn’t crash.

if (RandomQuestionIndex == Pastquestion && repeat == true)
        {
            while (RandomQuestionIndex == Pastquestion)
            {
                RandomQuestionIndex = 
                Random.Range(0,notAnsweredQuestions.Count);
            }
        }

Whole code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System.Linq;
using UnityEngine.UI;
public class TrueOrFalseQuizManager : MonoBehaviour
{
    private int totalquestions;
    public TextMeshProUGUI scoreText;
    private int QuestionsWrong = 0;
    public GameObject EndGameScreen;
    private bool EnteredIEnumerator;
    private bool EnteredIEnumerator2;
    public Animator CanvasAnim;
    private bool repeat = false;
    private bool End;
    public Image image;
    private int Pastquestion;
    private int RandomQuestionIndex;
    public TextMeshProUGUI tmpText;
    public QuestionHolder[] questions;
    private QuestionHolder currentQuestion;
    private  List<QuestionHolder> notAnsweredQuestions;

    void Start()
    {
        notAnsweredQuestions = questions.ToList<QuestionHolder>();
        RandomQuestionIndex = Random.Range(0, notAnsweredQuestions.Count);
        Pastquestion = RandomQuestionIndex;
        currentQuestion = notAnsweredQuestions[RandomQuestionIndex];
        tmpText.text = ("" + currentQuestion.Fact);
        image.sprite = currentQuestion.QuestionImage;
    }
    private void Update()
    {
        
        if(notAnsweredQuestions.Count == 0 && End == false)
        {
            End = true;
            Debug.Log("End");
            StartCoroutine(EndQuiz(1));
        }
    }
    
    
    IEnumerator EndQuiz(float time)
    {
        
        if (EnteredIEnumerator2)
            yield break;

        EnteredIEnumerator2 = true;
        yield return new WaitForSeconds(time);
        //code after delay
        totalquestions = questions.Length;
        QuestionsWrong = totalquestions - QuestionsWrong;
        if(QuestionsWrong < 0)
        {
            QuestionsWrong = 0;
        }
        scoreText.text = "" + QuestionsWrong + "/" + "" + questions.Length;
        EndGameScreen.SetActive(true);
        EnteredIEnumerator2 = false;
    }
    IEnumerator NewQuestion(float time)
    {
        
            if (EnteredIEnumerator)
                yield break;

            EnteredIEnumerator = true;

            yield return new WaitForSeconds(time);
            RandomQuestionIndex = Random.Range(0, notAnsweredQuestions.Count);
        /*if (RandomQuestionIndex == Pastquestion && repeat == true)
        {
            while (RandomQuestionIndex == Pastquestion)
            {
                RandomQuestionIndex = Random.Range(0, notAnsweredQuestions.Count);
            }
        }*/
        Pastquestion = RandomQuestionIndex;
        if (notAnsweredQuestions.Count > 0)
        {
            currentQuestion = notAnsweredQuestions[RandomQuestionIndex];
        }
            tmpText.text = ("" + currentQuestion.Fact);
            image.sprite = currentQuestion.QuestionImage;
            repeat = false;
            // Code to execute after the delay

            EnteredIEnumerator = false;
        
    }
    public void CorrectPressed()
    {
        if (currentQuestion.IsTrue == true)
        {
            Debug.Log("Yup");
            CanvasAnim.SetTrigger("True");
            if (End == false)
            {
                notAnsweredQuestions.Remove(currentQuestion);
                StartCoroutine(NewQuestion(1));
            }
        }
        else
        {
            Debug.Log("WRONG");
            QuestionsWrong += 1;
            CanvasAnim.SetTrigger("NOPE");
            repeat = true;
            if (End == false)
            {
                StartCoroutine(NewQuestion(1));
            }
        }
    }
    public void FalsePressed()
    {
        if (currentQuestion.IsTrue == false)
        {
            Debug.Log("Yup");
            CanvasAnim.SetTrigger("True");
            if (End == false)
            {
                notAnsweredQuestions.Remove(currentQuestion);
                StartCoroutine(NewQuestion(1));
            }
            
        }
        else
        {
            QuestionsWrong += 1;
            Debug.Log("WRONG");
            CanvasAnim.SetTrigger("NOPE");
            repeat = true;
            if(End == false)
            {
                StartCoroutine(NewQuestion(1));
            }
        }
    }

}

193810-ezgif-4-52343ecd7f.gif

It’s because the length of notAnsweredQuestions is zero, which makes RandomQuestionIndex always equal zero (which is the same as pastQuestion), and so the loop continues forever, crashing unity.


Add a check before you run the loop to check the length of notAnsweredQuestions. Don’t run the loop, if it’s zero.