Unable to instantiate gameobject after using Destroy()

Hello,

I have a script which allows me to create 3 balls: red, blue and green.
These orbs spawn at the available spawn positions : pos1, pos2 and pos3.
They follow the player, hovering over the player’s head.

The goal is to have a certain combination of balls and then press a button to create a power up.
When I press space and get the power up, I want to destroy the existing balls, create a spell and start over again ( be able to create more balls)
However, after I destroy the balls, I am unable to create them again. I am getting a MissingReferenceException. The problem happens when i try to use Destroy() in the Invoke function towards the very end of the script. I have no idea at which part of the code the problem is occurring exactly!

Here is the script im using.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class orbController : MonoBehaviour
{
	int currentOrb = 0;
	int maxOrbs = 3;

	public Transform[] spawnPos;

	public GameObject airOrb;
	public GameObject fireOrb;
	public GameObject waterOrb;

	//public bool allowOrbit = false; // if set to true then orbiting functionality needs to be added
	public bool overRideCurrentOrb = false; // if set to true then "currentOrb" will not be used

	public List<GameObject> orbs = new List<GameObject>();

	void Start()
	{
		if (maxOrbs < spawnPos.Length)
		{
			maxOrbs = spawnPos.Length;
		}
	}

	void Update()
	{
		if (Input.GetKeyDown ("q") && currentOrb < maxOrbs) 
		{
			spawnAirOrb ();

		}

		if (Input.GetKeyDown ("w") && currentOrb < maxOrbs) 
		{
			spawnFireOrb ();
		}

		if (Input.GetKeyDown ("e") && currentOrb < maxOrbs) 
		{
			spawnWaterOrb ();
		}
		InvokeSpell ();
	}

	void spawnAirOrb()
	{ 
		spawnOrb(airOrb,0);
	}

	void spawnFireOrb()
	{
		spawnOrb(fireOrb,1);
	}

	void spawnWaterOrb()
	{
		spawnOrb(waterOrb,2);
	}

	void spawnOrb(GameObject orb, int index)
	{ 
		if (orb != null)
		{
			if (!overRideCurrentOrb)
			{
				index = currentOrb;
			}

			GameObject oldOrb = GameObject.Find(orb.name + "(Clone)");
			Vector3 position = spawnPos [index].position;
			Quaternion rotation = spawnPos [index].rotation;

			if (orbs.Count == maxOrbs)
			{
				for (int i = 0; i < orbs.Count; i++)
				{
					if (( (orbs*.transform.position == position)) ||*

_ ((orbs != null)))_
* {*
_ oldOrb = orbs*;
orbs.RemoveAt(i);
position = oldOrb.transform.position;
rotation = oldOrb.transform.rotation;
break;
}
}
}*_

* if (oldOrb != null)*
* {*
* bool destroyOrb = (oldOrb.transform.position == position);*

* if (destroyOrb)*
* {*
* Destroy(oldOrb);*
* }*
* }*
* GameObject spawnedOrb = (Instantiate (orb, position, rotation) as GameObject);*
* spawnedOrb.transform.parent = spawnPos [index].transform;*

* orbs.Add(spawnedOrb);*

* currentOrb++;*
* if (currentOrb >= maxOrbs)*
* {*
* currentOrb = 0;*
* }*
* }*

* }*

* void InvokeSpell()*
* {*
* GameObject[] orb1 = GameObject.FindGameObjectsWithTag (“airOrb”);*
* GameObject[] orb2 = GameObject.FindGameObjectsWithTag (“fireOrb”);*
* GameObject[] orb3 = GameObject.FindGameObjectsWithTag (“waterOrb”);*

* if(Input.GetKeyDown (“space”))*
* {*
* if(orb1.Length==3)*
* {*
* Debug.Log(“3 air orbs bruh”);*
* //GameObject spell = (Instantiate (ball, player.position, player.rotation) as GameObject);*
* for (int i = 0; i <= orb1.Length; i++) {*
_ Destroy (orb1 .gameObject);
* currentOrb = 0;*_

* }*

* }*
* else if(orb2.Length==3)*
* {*
* Debug.Log(“3 fire orbs bruh”);*
* }*
* else if(orb3.Length==3)*
* Debug.Log(“3 water orbs bruh”);*
* }*

* }*
}

Invoker is sad BibleThump.

From what I see, you are trying to clone an object you have just destroyed. Use the original prefabs instead airOrb, fireOrb, waterOrb.

Hi @Diskodragon, Try the following out

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class OrbControl : MonoBehaviour
{
    int currentOrb = 0;
    int maxOrbs = 3;

    public Transform[] spawnPos;

    public GameObject airOrb;
    public GameObject fireOrb;
    public GameObject waterOrb;
    
    // public bool allowOrbit = false; // if set to true then orbiting functionality needs to be added
    public bool overRideCurrentOrb = false; // if set to true then "currentOrb" will not be used

    public List<GameObject> orbs = new List<GameObject>();

    void Start()
    {
        if (maxOrbs < spawnPos.Length)
        {
            maxOrbs = spawnPos.Length;
        }
    }
    
    void Update()
    {
        if (Input.GetKeyDown ("q") && currentOrb < maxOrbs) 
        {
            spawnAirOrb ();
        }

        if (Input.GetKeyDown ("w") && currentOrb < maxOrbs) 
        {
            spawnFireOrb ();
        }

        if (Input.GetKeyDown ("e") && currentOrb < maxOrbs) 
        {
            spawnWaterOrb ();
        }

        if(Input.GetKeyDown ("space"))
        {
            InvokeSpell ();
        }
    }

    void spawnAirOrb()
    { 
        spawnOrb(airOrb, 0);
    }

    void spawnFireOrb()
    {
        spawnOrb(fireOrb, 1);
    }

    void spawnWaterOrb()
    {
        spawnOrb(waterOrb, 2);
    }

    void spawnOrb(GameObject orb, int index)
    { 
        if (orb != null)
        {
            if (!overRideCurrentOrb)
            {
                index = currentOrb;
            }
            Vector3 position = spawnPos [index].position;
            Quaternion rotation = spawnPos [index].rotation;
            
            if (orbs.Count == maxOrbs)
            {
                Destroy(orbs[index]);
            }

            GameObject spawnedOrb = (GameObject)Instantiate (orb, position, rotation);
            spawnedOrb.transform.SetParent(transform, false);
            if (orbs.Count == maxOrbs)
                orbs[index] = spawnedOrb;
            else    
                orbs.Add(spawnedOrb);

            currentOrb++;
            if (currentOrb >= maxOrbs)
            {
                currentOrb = 0;
            }
        }
    }

    void InvokeSpell()
    {
        GameObject[] orb1 = GameObject.FindGameObjectsWithTag ("airOrb");
        GameObject[] orb2 = GameObject.FindGameObjectsWithTag ("fireOrb");
        GameObject[] orb3 = GameObject.FindGameObjectsWithTag ("waterOrb");
        if ((orb1.Length == maxOrbs) || (orb2.Length == maxOrbs) || (orb3.Length == maxOrbs))
        {
            for (int i = 0; i < orbs.Count; i++) {
                Destroy (orbs*);*

}
currentOrb = 0;
}
}
}