BakeMesh tip

Here’s a free tip for everyone:

When using the BakeMesh function:

Mesh mesh=new Mesh();
GetComponent<SkinnedMeshRenderer>().BakeMesh(mesh);

Don’t forget to destroy the mesh after you’ve finished using it:

Destroy(mesh)

Otherwise you might end up with a big memory leak! It doesn’t get automatically garbage collected. Well not in unity 2018 anyway.

2 Likes

Good tip. This also applies to other things like Materials and GUIStyles and random other bits that can be not connected to something in the scene.

It ought to go away if you call Resources.UnloadUnusedAssets() and you don’t have a reference to it, and it also should go away when you change scenes, which calls this same function.

1 Like