Prevent 'Cleaning up leaked objects'

I’ve been running into this problem with Unity 3.4 on OS X, and searching for this error message only reveals that it’s supposed to be a harmless optimization. My problem is that it’s NOT harmless, as the “leaked” objects are not actually leaked. Here’s the full message:

Cleaning up leaked objects in scene since no game object, component, or manager is referencing them
 X has been leaked Y times.

Here are two examples where I get this log message in relation to objects NOT in the scene being wrongfully destroyed:

  1. I have a ScriptableObject that contains an array of other ScriptableObjects which are not referred to in any other place. When saving the scene, the other ScriptableObjects disappear.
  2. I have a prefab with a dynamically built mesh. When saving the scene I get this message, and the mesh is lost from the MeshFilter component.

In all cases, I get the same message every time I save and the object(s) are not destroyed until the next play test or if the scene is reloaded. After that the objects are lost and the message doesn’t appear for future saves.

My questions: How can I disable this “feature”? Is this a bug in Unity or am I doing something wrong?

I think you should look at your hideFlags for the auto-generated stuff. If you want to re-create them every time anyway, they most probably should be “DontSave” or even “HideAndDontSave”.

I recommend this talk to get a found understanding of the serialization system: