::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;
void OnDestroy()
{
float randomNumber = UnityEngine.Random.Range(0f, 100f);
List possibleDrops = new List();
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;
bool isQuitting = false;
void OnApplicationQuit() { isQuitting = true; }
void OnDestroy()
{
if(isQuitting) return;
float randomNumber = UnityEngine.Random.Range(0f, 100f);
List possibleDrops = new List();
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);
}
}
}