- Home /
Unity not unloading procedurally generated mesh when parent GameObject is destroyed and its not referenced
I've seen this pop up a number of times but haven't seen a definitive answer (here, here, here, etc). In my case I'm simply generating a mesh, attaching it to a gameobject, and when it comes time I destroy that gameobject, nothing too special.
However, I noticed in the memory profiler the meshes weren't actually being unloaded and memory would accumulate forever. I've tried every unload method I could find but nothing works except Resources.UnloadUnusedAssets(). But I feel like you really shouldn't have to call that almost every frame and there should be some way to unload the mesh properly.
As you can see in the attached picture there aren't even any references to these meshes so why are they sticking around? Furthermore, I also call .clear() on every mesh that I attempt to destroy but you can see in the pic they are still just as large as the other proper meshes.
Some code snippets I've tried (in many different orders etc):
if (chunk.GetComponent<Mesh>() != null) chunk.GetComponent<Mesh>().Clear();
chunk.SetActive(false);
SkinnedMeshRenderer.Destroy(chunk.GetComponent<Mesh>());
SkinnedMeshRenderer.DestroyObject(chunk.GetComponent<Mesh>());
GameObject.DestroyImmediate(chunk.GetComponent<Mesh>());
AssetBundle.Destroy(chunk.GetComponent<Mesh>());
Resources.UnloadAsset(chunk.GetComponent<Mesh>());
GameObject.Destroy(chunk.GetComponent<Mesh>());
GameObject.Destroy(chunk);
SkinnedMeshRenderer.Destroy(chunk);
SkinnedMeshRenderer.DestroyObject(chunk);
GameObject.DestroyImmediate(chunk);
AssetBundle.Destroy(chunk);
Resources.UnloadAsset(chunk);
GameObject.Destroy(chunk);
$$anonymous$$esh isn't a component, you can't use GetComponent(). You can Destroy($$anonymous$$eshfilter.shared$$anonymous$$esh), it force object to unload, BUT other linked objects will disappear to
Your answer
![](https://koobas.hobune.stream/wayback/20220612074731im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Memory usage increase after a mesh rewrite??? 1 Answer
Mesh memory leak 3 Answers
0 or Blank Ref Count Items in Memory Profiler 0 Answers