MissingReferenceException error

MissingReferenceException: The object of type 'RecycleGameObject' 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.

whenever i reset my scene by using SceneManager.LoadScene(2); this error is shown .
do you know ho to make this error go away ?

The error pretty much says it all.

One of your RecycleGameObject instances has been destroyed, yet you’re still trying to access it afterwards.
I’m afraid you have to look for that specific instance by yourself, or post some code that is related to the issue.

i used a tutorial to create an endless runner .this is the code , one of them ,

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ObjectPool : MonoBehaviour {

    public RecycleGameObject prefab;

    private List<RecycleGameObject> poolInstances = new List<RecycleGameObject>();

    private RecycleGameObject CreateInstance(Vector3 pos){

        var clone = GameObject.Instantiate (prefab);
        clone.transform.position = pos;
        clone.transform.parent = transform;

        poolInstances.Add (clone);

        return clone;
    }

    public RecycleGameObject NextObject(Vector3 pos){
        RecycleGameObject instance = null;

        foreach (var go in poolInstances) {
            if(go.gameObject.activeSelf != true){
                instance = go;
                instance.transform.position = pos;
            }
        }

        if(instance == null)
            instance = CreateInstance (pos);

        instance.Restart ();

        return instance;
    }

}

when i reset game scene it occurs and next time there is no enemy in scene but that error in console. thanks .

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour
{

    public GameObject GameoverPanel;
    public GameObject Buttons;
    public PlayerMovement pm;
    public bool notcrashed = true;
    public Animator anim;
    public GameObject InGamepausePanel;

    public GameObject PlayerPrefab;
    private GameObject player;


    void Awake()
    {
        anim = GameObject.Find ("Player").GetComponent<Animator> ();
    }
    void Start()
    {
      
    }

    public void OnGameReset()
    {
        GameoverPanel.SetActive (false);
        Buttons.SetActive (true);
        SceneManager.LoadScene (2);
        Time.timeScale = 1;
        InGamepausePanel.SetActive (false);
    }

    public void Restart()
    {
        StartCoroutine (retro ());

    }
    IEnumerator retro()
    {
        Buttons.SetActive (false);
        pm.isPaused = false;
        notcrashed = false;
        var running = false;
        anim.SetBool ("Running", running);
        yield return new WaitForSeconds(2f);
        Time.timeScale = 0;
        gameoverPanel ();

    }
        public void gameoverPanel()
        {
        GameoverPanel.SetActive (true);
        }
}

You have to find the pieces of code that

  • access the generated recycables ( you might be accessing one that has been destroyed)
  • destroy the recycable objects (the best would be if that is handled by the pool, or at least the pool will be notified of an extern destruction)

It’s not that easy to say where that occurs.

i think this is that peice , but how to manage that instance ?

      foreach (var go in poolInstances) {
            if(go.gameObject.activeSelf != true){
                instance = go;
                instance.transform.position = pos;
            }
        }

Use the debugger to step through it or print some logs to the console to be sure it happens there. Otherwise it’s just trial and error to find the bug.

As mentioned earlier, it’s hard to tell where the exception happens.
Do you keep the ObjectPools gameobject alive during scene loading? Does every scene create a new pool? Do you know (by any chance) if the exception is still thrown when you leave the old scene or does it already happen in the new scene?
If that part is the root of the exception, then you either keep the ObjectPool alive and access it in the new scene, which would then only contain references to no longer existing instances (because they have been destroyed during scene loading).

Or you’re generally destroying an object and do not remove it from the ObjectPool. Then that error might rather occur in the old still before loading.

I can only repeat myself as you notice. We know nothing about the program flow so it’s just lots of guessing. The only thing that can be said is exactly what the error message has already stated… You attempt to use an object that has already been destroyed.

You might want to extend the ObjectPool so that it either gets notified of an instance’s destruction so that it can immediately remove it from the pool, or you manually remove them by doing null-checks whenever you need a new object.
First solution sounds better though.

yes , it is live during scene loading .
no this pool is only available in current scene .
when i leave this scene and come back to it , the error happens .
as you said it destroy an object but does not remove it from game object pool .