0
votes

I've been building a quiz game that randomly picks a gameobject from a list and after a question is completed it reloads the scene for a new question however, it states this error:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

GameManager.Start () (at Assets/Scripts/GameManager.cs:30)

And this is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.SceneManagement;
 
public class GameManager : MonoBehaviour {
 
    public static int betul1 = 0;
    public static int betul2 = 0;
    public static int salah1 = 0;
    public static int salah2 = 0;
 
    public GameObject[] questions;
    private static List<GameObject> unansweredQuestions;
    private GameObject currentQuestion;
 
    [SerializeField]
    private float transitionTime = 1f;
 
    void Start()
    {
       
       if (unansweredQuestions == null || unansweredQuestions.Count == 0)
         {
             unansweredQuestions = questions.ToList<GameObject>();
         }
       
        GetQuestion();
        currentQuestion.SetActive(true);
    }
 
    void GetQuestion()
    {
        int randomNumber = Random.Range(0,unansweredQuestions.Count);
        currentQuestion = unansweredQuestions[randomNumber];
    }
 
    IEnumerator NextQuestion()
    {
 
        unansweredQuestions.Remove(currentQuestion);
        //currentQuestion.SetActive(false);
   
        yield return new WaitForSeconds(transitionTime);
 
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
 
    public void Yes()
    {
        if (betul1 == 1 && betul2 == 1)
        {
            Debug.Log("Congratulations! You're correct!");
            StartCoroutine(NextQuestion());
        }
 
        if (salah1 == 1 && salah2 == 1)
        {
            Debug.Log("Sorry! You're wrong!");
            StartCoroutine(NextQuestion());
        }
 
        if (betul1 == 1 && salah2 == 1)
        {
            Debug.Log("Your answer is invalid. Please fix it.");
        }
 
        if (betul2 == 1 && salah1 == 1)
        {
            Debug.Log("Your answer is invalid. Please fix it.");
        }
    }
}

I'm not sure what's wrong about it. I'm still relatively new to Unity so I would really appreciate it if you could point out what's causing this. Thank you in advance.

3
How is built and feed questions ?Cid
Considering the documentation int randomNumber = Random.Range(0,unansweredQuestions.Count); can throw an IndexOutOfRangeException Consider using int randomNumber = Random.Range(0, unansweredQuestions.Count - 1); (the max value is included in Random.Range(min, max)Cid

3 Answers

0
votes

The error explains it all. On the first run of your game you'll find that GameManager.cs is attached to a valid GameObject and running fine. But when you reload a new scene all objects in a scene are destroyed and the second scene is loaded.

So there is no more GameManager context. The GameObject which your GameManager.cs script was associated with is destroyed. Since all data in GameManager.cs is static, I would suggest making it a static class or, if you want to keep the object around, use DontDestroyOnLoad

0
votes

If you don't destroy any objects, maybe some of the objects in the unansweredQuestions list are destroyed when you reload the scene. So when you get the reference from GetQuestion(), it returns a reference to destroyed object. So when you try to set it active it throws this exception.

You can easily fix this by checking currentQuestion for null after getting it in GetQuestion().

But its better to fix the cause of the destroying of objects.

Try removing the currentQuestion from the unansweredQuestions, right after you get it in GetQuestion().

If there are other scripts accessing questions list, the problem may be there. If the objects that are in questions list are destroyed, the objects in unansweredQuestions will be destroyed too.

0
votes

*Edit. ok so nvm the GameManager. the error is on line 30, denoted by your output.

line 30 is: currentQuestion.SetActive(true);

this error is saying that currentQuestion is empty. if your reloading the scene you will need to set this to a question, in void Start() before trying to set it to active.