EditorScript: dynamic creation of mesh stored in a prefab fails

Hi all,

I’m writing an editor script that does the following:

  • create a gameobject
  • create a mesh and assign it to the game object via MeshFilter and MeshRenderer
  • create a material and assign to the mesh
  • create an empty prefab and connect my newly created game object to this new prefab

Once this is done, I now have a prefab in my project’s assets folder, that contains a gameObject that has a mesh and material. This part works fine, I can see my instantiated prefab in the scene, just like I expect it, and create other instances if needed.

The problem is, when I close/reopen the project, the gameObject has lost its mesh :frowning:
Same thing happens if I try to export the prefab as a unitypackage for another project, or export it as an assetbundle.

Am I missing something?

What I’m basically trying to do is create some prefabs with generated geometry, that I can then save in asset bundles for use in my project.

Tks for any help

I think you may need to save the mesh itself as a mesh asset, and then have the game object (and/or prefab) reference it as a shared mesh.

If you check the scripts wiki, there’s a script there somewhere that shows how to create and save a mesh procedurally (the example script creates a custom ‘plane’ mesh and saves it as an asset).