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
- This topic has 1 reply, 1 voice, and was last updated 6 months, 1 week ago by Terence.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
June 14, 2024 at 10:59 pm #15103::
@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;
June 14, 2024 at 11:02 pm #15104::As the error message reveals, the issue is caused by
OnDestroy()
onDropRateManager
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); } } }
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.
Advertisement below: