Fix? 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.

I cant fix this problem-

“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.”

When I double click on it, it brings me to this area of code-

    public GameObject GetPoolObject() {
        for (int i = 0; i < pooledObjects.Count; i++) {
            // Get first non active pooled object.
            if (!pooledObjects*.activeInHierarchy) {*

return pooledObjects*;*
}
}
This line is highlighted-
if (!pooledObjects*.activeInHierarchy) {*
Btw the type of game im making is a endless runner.
Please help ive been trying to figure it out for hours.

I solved that problem with-

if (gameObject != null)

but there is a new error-

error CS0844: A local variable gameObject' cannot be used before it is declared. Consider renaming the local variable when it hides the member UnityEngine.Component.gameObject’

when double click on this area of code comes up-

    public GameObject GetPoolObject() {
        for (int i = 0; i < pooledObjects.Count; i++)

        {
            // Get first non active pooled object.
            
                if (!pooledObjects*.activeInHierarchy)*

if (gameObject != null)
{
return pooledObjects*;*

}
}

This line is highlighted-

if (gameObject != null)
Help please

Hey do you have the answer already?

Hey do you have the answer already?

the easy way:

use “try catch” could solve the problem.

try{ 
             if (!pooledObjects*.activeInHierarchy) {*

return pooledObjects*;*
}
}
catch{
//continue or return
}