Deletes 2 Scriptable objects after reading instead of 1

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;
        }

    }
}

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

You must find a way to get the information you need in order to reason about what the problem is.