Forum begins after the advertisement:


[Part 5] Some objects were not cleaned up when closing the scene

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 5] Some objects were not cleaned up when closing the scene

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #15103
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    @calymv3686 asks this question on YouTube:

    The problem I got is when I stop the game I will get a gem on the map and an Error in Unity Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?) The following scene GameObjects were found: Blue Gem(Clone)

    And the resolve was

    private bool isQuitting = false;
    
    private void OnApplicationQuit() {
        isQuitting = true;
    }
     if (isQuitting) return;
    #15104
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    As the error message reveals, the issue is caused by OnDestroy() on DropRateManager spawning new GameObjects (see highlighted part below), as Unity is unable to destroy them when quitting:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class DropRateManager : MonoBehaviour
    {
        [System.Serializable]   //Serialize the class
        public class Drops
        {
            public string name;
            public GameObject itemPrefab;
            public float dropRate;
        }
    
        public List<Drops> drops;
    
        void OnDestroy()
        {
            float randomNumber = UnityEngine.Random.Range(0f, 100f);
            List<Drops> possibleDrops = new List<Drops>();
    
            foreach (Drops rate in drops)
            {
                if (randomNumber <= rate.dropRate)
                {
                    possibleDrops.Add(rate);
                }
            }
            //Check if there are possible drops
            if(possibleDrops.Count > 0)
            {
                Drops drops = possibleDrops[UnityEngine.Random.Range(0, possibleDrops.Count)];
                Instantiate(drops.itemPrefab, transform.position, Quaternion.identity);
            }
        }
    }

    To fix this, you will need to track if the application is quitting, and prevent the creation of new GameObjects by OnDestroy() if so:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class DropRateManager : MonoBehaviour
    {
        [System.Serializable]   //Serialize the class
        public class Drops
        {
            public string name;
            public GameObject itemPrefab;
            public float dropRate;
        }
    
        public List<Drops> drops;
        bool isQuitting = false;
    
        void OnApplicationQuit() { isQuitting = true; }
    
        void OnDestroy()
        {
            if(isQuitting) return;
            float randomNumber = UnityEngine.Random.Range(0f, 100f);
            List<Drops> possibleDrops = new List<Drops>();
    
            foreach (Drops rate in drops)
            {
                if (randomNumber <= rate.dropRate)
                {
                    possibleDrops.Add(rate);
                }
            }
            //Check if there are possible drops
            if(possibleDrops.Count > 0)
            {
                Drops drops = possibleDrops[UnityEngine.Random.Range(0, possibleDrops.Count)];
                Instantiate(drops.itemPrefab, transform.position, Quaternion.identity);
            }
        }
    }
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: