Why does some of script fail to execute after a copied object instance has been destroyed?

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

    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;
    [Range(0, 10)] public int maxGoldSpawnAmount = 5;
    [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);


    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);
        for (int i = 0; i < randOutComeForGold - 1; i++)
            var Gclone = Instantiate(GoldOrb, transform.position, transform.rotation);
        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;


This is likely happening because, after the method executes once, the GameObject which your object is attached to is destroyed. This means that the references you have made to XpOrb and GoldOrb have also been destroyed, and you can no longer instantiate clones of them. Try running this script after commenting out the line:

// this.enabled = false;

If it works, I suggest destroying the script another way. Perhaps create a counter and only destroy the script once the desired count is reached.

You instantiate gameobjects and add them to the _clones list . When you destroy those gameobjects, do you make sure they are removed from this list? In unity there is no way to check if the script you are accessing lives on a destroyed object, so you need to make sure when you destroy your objects, all references to those objects need to be notified that the gameobject component is destroyed. In other words, when you call cloneObj.transform.x, you might be getting this error as it is possible that the cloneObj is destroyed, but your reference to it is not.