Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Some objects were not cleaned up when closing the scene

Discussion in 'Scripting' started by Asough, May 22, 2024.

  1. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    I get an error message that says Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)
     
    Last edited: May 24, 2024
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,391
    Well, do you have anything instancing game objects in OnDestroy? That should be an easy question to answer.
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,098
    As the message says, likely something gets instantiated from OnDisable/OnDestroy while Unity stopping play mode, which is not supported. Unfortunately, the message is unspecific and doesn't tell you which object was instantiated or where it was instantiated from (likely, this comes from a final check after all OnDestroy callbacks have already been called, at which point it's not known where they came from).

    If you don't have an idea where this could be happening, you can only try to narrow it down by disabling/removing parts of your scene until the message no longer appears. Note that instantiating doesn't necessarily need to happen directly in OnDisable/OnDestroy, it can also be triggered indirectly through events or some other system, in a way that's not immediately obvious.

    Unfortunately, there's also no good way to detect whether OnDisable/OnDestroy is called normally during gameplay or during shutdown. So, if you want to e.g. instantiate some particles when a game object is destroyed, you can't really do that in a safe way that will never trigger this message. You'd have to call another method on the game object before it's being destroyed and avoid instantiating in OnDisable/OnDestroy all together.

    On the other hand, I don't think this message is very serious. If it only happens rarely, you should be able to ignore it and the instantiated object will be collected by Unity's garbage collection.
     
  4. Asough

    Asough

    Joined:
    Sep 12, 2022
    Posts:
    177
    It's caused by this script:

    Code (CSharp):
    1.     public void OnDestroy()
    2.     {
    3.         Victory.StartVictory();
    4.     }
    It's a script that I have attached to an asteroid in my game. It's supposed to load a victory scene, after the asteroid has been destroyed. If I remove this script from the asteroid, the error message no longer appears. If I attach the script to the asteroid again, then the error message comes back?

    Anyway, I was able to solve it. I got rid of that script, then I opened up the asteroid's health bar script and created an if statement, that plays the victory scene after the asteroid has been destroyed.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Health : MonoBehaviour
    6. {
    7.     public int curHealth = 100;
    8.     public int maxHealth = 100;
    9.  
    10.     public void Start()
    11.     {
    12.         curHealth = maxHealth;
    13.     }
    14.  
    15.     public void Update()
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.Space))
    18.         {
    19.             DamageAsteroid(10);
    20.         }
    21.  
    22.         if (curHealth <= 0)
    23.         {
    24.             Destroy(gameObject);
    25.         }
    26.  
    27.         if (curHealth <= 0)
    28.         {
    29.             Victory.StartVictory();
    30.         }
    31.     }
    32.  
    33.     public void DamageAsteroid(int damage)
    34.     {
    35.         curHealth -= damage;
    36.     }
    37. }
    The error message no longer appears, and the victory scene still loads after the asteroid has been destroyed.
     
    Last edited: May 24, 2024