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[i].transform.position == position)) ||
((orbs[i] != null)))
{
oldOrb = orbs[i];
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 [i].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");
}
}
}
Answer by Socapex · Dec 30, 2016 at 07:11 PM
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.
Answer by TBruce · Dec 31, 2016 at 10:42 PM
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[i]);
}
currentOrb = 0;
}
}
}
Your answer

Follow this Question
Related Questions
Can I disable Reflection Probes in shader? 0 Answers
Help me destroy anplayer when out of bounds. 2 Answers
Networking, destroying an object only on one of the clients 0 Answers
Why Isn't My Break Glass Script Working? 1 Answer
How do I destroy multiple game objects in a specific range of my player? 0 Answers