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[i].activeInHierarchy) {
return pooledObjects[i];
}
}
This line is highlighted-
if (!pooledObjects[i].activeInHierarchy) {
Btw the type of game im making is a endless runner.
Please help ive been trying to figure it out for hours.
Answer by MysticsMyth · Jun 25, 2018 at 09:26 PM
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[i].activeInHierarchy)
if (gameObject != null)
{
return pooledObjects[i];
}
}
This line is highlighted-
if (gameObject != null)
Help please
public GameObject GetPoolObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
// Get first non active pooled object.
if (pooledObjects[i] != null && !pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
}
Answer by Yuan-Lee · Oct 29, 2021 at 04:25 AM
the easy way:
use "try catch" could solve the problem.
try{
if (!pooledObjects[i].activeInHierarchy) {
return pooledObjects[i];
}
}
catch{
//continue or return
}
Your answer
Follow this Question
Related Questions
My game script is fine but I'm still getting unexpected class error 0 Answers
Survival Shooter Error CS0120 1 Answer
Why does my Melee System not work? 0 Answers
Issues With Touch Rotation code 0 Answers
Null Reference Exception Error 1 Answer