"Some objects were not cleaned up when closing the scene"

I'm getting the following error:

Some objects were not cleaned up when closing the scene

Error in file ....\Runtime\Misc\SaveAndLoadHelper.cpp at line: 161

Now I don't think this message could be any less helpful. Unfortunately, no one else seems to have had this problem before. I'm unable to isolate the error, as it's a very complex scene. Does someone know what could cause an object to not be cleaned up?

In order to resolve this error message, make sure to check if the script is enabled, for example:

public void OnDisable(){
    if(this.enabled){
        // Do stuff.
    }    
}

This will ensure it only runs OnDisable when the object is destroyed which is what you wanted in your behaviour.

I ran into this same error and figured out my issue today. It turns out my problem was I was in instantiating a death effect in OnDestroy(). I am presuming this was putting the new object into the scene graph after the graph was copied to a temporary variable to be iterated over for destroying things, or possibly screwing with an iterator being used to traverse the graph or something. In any case, the object wasn’t getting cleaned up.

So, the short version - don’t instantiate stuff in OnDestroy().

Hum. In a silly attempt to clean up my objects, I had added this code to a sometimes used object:

public void OnDisable()
{
    if (renderer.material != null)
    {
        Destroy(renderer.material);
    }
}

Removing this function fixed the problem. Turns out I shouldn't meddle with these things.

These answers did not solve the problem, but instead, I eliminated the OnDestroy call because my code is simple enough.

  void OnCollisionEnter(Collision collision) {
      Destroy(gameObject);
  }

  void OnDestroy() {
    Instantiate();
  }

becomes

  void OnCollisionEnter(Collision collision) {
      addExplosion(); //
      Destroy(gameObject);
  }

  void addExplosion() { //
    Instantiate();
  }

This can happen when you make a MonoBehaviour that has a name that does not match its file name. Normally Unity will warn when you try and add the component to a GameObject via the inspector however it will not warn you when you use AddComponent() at runtime.

I hope this might be helpful. I just found and fixed this same symptom with the unhelpful “Some objects were not cleaned up when closing the scene” message. It dogged me for hours, as there was no help online that addressed my problem. I recently installed and am using EZGUI and wondered if it might be the source of my problem. I found that after binary search debugging, disabling gameobjects in my scene, I narrowed the problem to an object with two UIButton components. It would not be fair to blame EZGUI for this bug per se, but clearly there is a problem when two or more components occupy the same object, which could be said of many other component combinations.

That’s how we’ve worked it out in RageSpline. One line of code really :slight_smile: Apparently we weren’t succeeding before because we were assigning the mesh data to a variable before calling DestroyImmediate on it (typical coder “make it legible” stuff) but that was leaving a reference in memory even after using the command. Doh. That’s with 3.5.3 btw.

public void OnDestroy () { DestroyImmediate(GetComponent().sharedMesh); }

I personally use OnBecameInvisible, though not all scenarios allow this.
Anyway, i think Unity really needs to add a new MonoBehavior like “OnDestroyRuntime” or something.