Hello, I’ve got an issue with this script, to summarise this script: it spawns a random amount of objects, adds them to a list and applies properties to the listed objects then disables the script. The issue I’m facing is once one of the objects that triggers the script has been destroyed (executed on a separate script) all other SpawnOnCollide components attached to identical objects fail to execute the foreach and anything below. Unity also complains that The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
public class SpawnOnCollide : MonoBehaviour
{
//Add probability to collectable spawn
//misc
private Rigidbody2D rb;
private Collider2D player;
private bool spawn = false;
private MoveToPlayer mtp;
private GameObject[] vase;
private GlobalCurrencies GC;
private GameObject Currencies;
//XP orb properties
private Vector3 scaleXp;
private int randOutComeForXp;
private int randOutComeForGold;
//Objects to instantiate
public GameObject XpOrb;
public GameObject GoldOrb;
public int value;
//chances of instatiations and intatiate amount
[Range(0, 10)] public int MaxXpSpawnAmount = 1;
[Space]
[Range(0, 10)] public int maxGoldSpawnAmount = 5;
[Space]
[Range(0, 2)] public int maxShardSpawnAmount = 1;
public List<GameObject> _clones = new List<GameObject>();
void Start()
{
Currencies = GameObject.FindWithTag("CurrencyManager");
GC = Currencies.GetComponent<GlobalCurrencies>();
player = GameObject.FindWithTag("Player").GetComponent<Collider2D>();
vase = GameObject.FindGameObjectsWithTag("vase");
mtp = GetComponent<MoveToPlayer>();
}
// Update is called once per frame
void Update()
{
if (spawn == true)
{
spawn = false;
randOutComeForXp = Random.Range(0, MaxXpSpawnAmount);
randOutComeForGold = Random.Range(0, maxGoldSpawnAmount);
XpSpawn();
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
spawn = true;
}
}
void XpSpawn()
{
for (int i = 0; i < randOutComeForXp - 1; i++)
{
var XpClone = Instantiate(XpOrb, transform.position, transform.rotation);
_clones.Add(XpClone);
}
for (int i = 0; i < randOutComeForGold - 1; i++)
{
var Gclone = Instantiate(GoldOrb, transform.position, transform.rotation);
_clones.Add(Gclone);
}
foreach (GameObject cloneObj in _clones)
{
value = Random.Range(1, 3);
scaleXp = new Vector2(cloneObj.transform.localScale.x, cloneObj.transform.localScale.y) * value * 0.25f;
cloneObj.transform.localScale = scaleXp;
Physics2D.IgnoreCollision(cloneObj.GetComponent<Collider2D>(), player.GetComponent<Collider2D>());
Physics2D.IgnoreCollision(cloneObj.GetComponent<Collider2D>(), cloneObj.GetComponent<Collider2D>());
foreach(GameObject _collectable in vase)
{
Physics2D.IgnoreCollision(cloneObj.GetComponent<Collider2D>(), _collectable.GetComponent<Collider2D>());
}
rb = cloneObj.GetComponent<Rigidbody2D>();
Vector2 direction = new Vector2((float)Random.Range(-70, 70), (float)Random.Range(80, 85));
float force = (float)Random.Range(5, 8);
rb.AddForce(direction * force);
}
this.enabled = false;
}
}