About Mesh object from MeshFilter.mesh is not destroyed.

I am using MeshFilter.mesh property in my script.
my problem is when I get the property value , I can see the number of mesh object increases continously when I run the same logic..
I checked the gameboject that holds the MeshFilter component was successfully destoryed.. but the mesh object was still not destoryed.. is there any way to avoid the leak issue?

MeshFilter.mesh returns an instance (copy) of the mesh when you access it. Accessing it multiple times is going to make multiple copies, and as these are UnityEngine.Objects, they are managed objects you need to destroy when no longer needed.

Usually you access the property once to get that instance, and destroy the instance when no longer needed.

Best to refer to the docs for this sort of thing: https://docs.unity3d.com/ScriptReference/MeshFilter-mesh.html

Adding onto Spiney's excellent post...

Usually the use cases are things like:

  • you have different meshes you want to assign, so you keep those Mesh references in your code and assign them to the MeshFilter when needed

  • you have different geometry (verts, tris, etc.), so you keep those arrays and feed them into the Mesh instance that you retrieved (or created!) and assigned to the MeshFilter

What exactly is your use case for manipulating meshes?

If you want more procgen examples, have a look around my MakeGeo project.

MakeGeo is presently hosted at these locations: