I have a component that procedurally generates its mesh. Its GameObject is based on a prefab. The (re)generation looks like that:
DestroyImmediate(mf.sharedMesh);
var mesh = new UnityEngine.Mesh { name = $"{name} (Generated Mesh)" };
updatedMesh.ApplyToUnityMesh(mesh);
mf.sharedMesh = mesh;
Now, when I instantiate the prefab, the generation runs and everything is fine. I can also change parameters and the mesh gets correctly regenerated. However, if I save the scene, there are now two meshes in the .unity file.
--- !u!43 &1699566314
Mesh:
m_Name: Rubber1 (Generated Mesh)
...
--- !u!43 &1050816235
m_Name: Rubber1 (Generated Mesh)
...
--- !u!1001 &1084441717
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 241506051}
m_Modifications:
- target: {fileID: 4392546155774236836, guid: 073cf4ebd8b550b4699ac09545ec5673,
type: 3}
propertyPath: m_Mesh
value:
objectReference: {fileID: 1699566314}
So one of the meshes is referenced in the prefab modifications, and the other looks like an orphan.
Does anyone have an idea what the cause of this might be?