I am trying to destroy a mesh renderer from my scene but once in a while I get this error:
MissingReferenceException: The object of type 'MeshRenderer' 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.
Here is my code:
internal void DestroySelf()
{
if (Occupied)
{
if (Structure != null)
GameObject.Destroy(Structure.gameObject);
}
GameObject.Destroy(sprite.gameObject);
}
maybe instead of destroying it set in to enabled=false. Is that error coming from the script you show here or is another script trying to access the MeshRenderer? Your script above doesn’t mention anything about the MeshRenderer only the object. So, maybe another script trying to access the meshRenderer of the destroyed object. Double click on the error and should take you to the script and line of code trying to access the renderer and add the != null condition there
Hi @egjlmn123 . The actual problem is not the Destroy. Perhaps you are trying to access the MeshRenderer from a script after destroying and a MissingReferenceException is being raised for that reason.