Cleaning up leaked objects in scene since no game object, component or manager is referencing them

I receive this error quite frequently - it seems random to me. What are "leaked objects" - what are some common causes of this error?

"Cleaning up leaked objects in scene since no game object, component or manager is referencing them"

On this thread in the forum: http://forum.unity3d.com/threads/6591-Cleaning-up-leaked-objects

Samantha says:

This is an internal message related to how we're handling meta data. It's actually a message informing you that some optimizations have been made. One could argue it's not necessary, but for now it's not hurting anything. A simple clear of the Console will get rid of the error.rid of the error.

This message has to do with Garbage Collection, which automatically finds unused memory and makes it available for re-use. The Scripting Behind the Scenes talk has more information about how that works in Unity.

Most objects are attached to GameObjects, which are seen as ‘roots’, so they’re automatically marked as being in-use. But this is not the case for objects like ScriptableObject, which you might need to create assets or to store data for editor tools.

During an Assembly Reload (e.g.: after compiling, when switching playmode or when loading a scene. See this blog post about Serialization), the garbage collector runs. Any assets that don’t have their HideFlags set to a variation of HideAndDontSave, and aren’t referenced by another ‘root’ such as a GameObject, are ‘cleaned’.

Usually when this happens, the message will show up every time you save changes to a scene, even if no new objects have been leaked, until you reload the scene. I don’t know why this happens, but I guess the ‘cleaned’ objects aren’t actually destroyed. And unfortunately the error message doesn’t give any information about which objects were leaked, or where they were created (Although to be fair, that doesn’t seem like a trivial problem to solve).

##Finding the cause##

To find the origin of the problem, you can incrementally remove scripts(don’t forget about custom windows, inspectors and menu items that might run), reload the scene, make a trivial change to the scene (add an empty GameObject), and save. The last thing you removed before the error disappeared then probably created an instance without setting its HideFlags.

It is important to note that if you set an Object’s HideFlags to HideAndDontSave, you are responsible for freeing the memory its memory when it’s no longer needed using DestroyImmediate.

since Unity 3.5.2 there exist a possiblity to unload unused assets

  • static function UnloadAsset (assetToUnload : Object) : void
  • static function UnloadUnusedAssets () : AsyncOperation

http://docs.unity3d.com/Documentation/ScriptReference/Resources.html

When deleting the object you should also call DestroyImmediate(object to destroy) on each object you want cleaned up, if you are using procedural materials on an object, then i would call DestroyImmediate(Material) to get rid of this message.