Non persistent Components, dynamically created and used in Editor, but not serialized

Hi guys,

I have a GameObject with a custom script as a MonoBehavior. This script is running in editor using attribute [ExecuteInEditMode]
This script is creating a mesh geometry on the fly, in method OnWillRenderObject, and as needed also adds appropriate components (MeshFilter, MeshRenderer) on the GameObject.

I don’t want those components (MeshFilter, MeshRenderer) to be serialized as the geometry might be rather big, and also because it is created on the fly, each time the script needs it.

For that I’m using hideFlags on each components by doing :

MeshFilter filter = gameObject.GetOrCreateComponent<MeshFilter> ();
MeshRenderer renderer = gameObject.GetOrCreateComponent<MeshRenderer> ();
filter.hideFlags |= HideFlags.HideAndDontSave;
renderer.hideFlags |= HideFlags.HideAndDontSave;

This has for effect to remove the component from the serialization.

But when I load my scene, I now get the following error messages :

  • Object GameObject (named ‘MyGameObject’) has multiple entries of the same MeshFilter component. Removing it!
  • Component MeshFilter could not be loaded when loading game object. Cleaning up!

I have no idea how to fix those error messages that happen on scene loading. I understand that the MeshFilter missing from serialization has something to do with it but I can’t understand the exact problem and how to fix it.

I guess this could be due to some Unity internal/optimization on serialization specific to the MeshFilter.

The Editor.log file gives the callstack from where the code is writting that message but it doesn’t really help me.
Both error message are written from method UnityEditor.AssetDatabase:OpenAsset(Int32, Int32).

Is there anyone that have already encountered those messages and that have a clear understanding of what’s going on exactly ?

In advance, thank you for any help.

I found that my biggest problem was that I was creating a Mesh object dynamically (that I binded to the MeshFilter) without setting the HideFlags.HideAndDontSave on that Mesh. The Mesh asset was stored in my scene build on save (and probably in the final plateform build) and produces huge scene files.

And also it produces a warning message about resulting leaks.

Using HideFlags.HideAndDontSave has resolved my main issue.

More info about that leak issue here : http://www.madpixelmachine.com/2013/07/05/monobehaviour-lifecycle-and-leaks-explained/