Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Diskodragon · Dec 30, 2016 at 02:58 PM · destroy objectinstancing

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");
         }
 
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Diskodragon · Dec 30, 2016 at 07:13 PM 0
Share

Haha Agreed RIP forge spirit

avatar image
0

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;
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

86 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges