• 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 /
avatar image
0
Question by RoloJo · Mar 04, 2013 at 03:38 PM · destroymissingreferenceexceptionclones

How do I destroy a specific clone of a GameObject?

I am trying to instantiate prefabs and then be able to walk around with a first person controller and destroy the ones I deem unworthy.

 public GameObject RabbitObj;
 
 void Start()
 for (int i=0;i<=RabbitInit;i++)
         {
             Position.x=(float)(UnityEngine.Random.value-0.5)*90f+50f;
             Position.y=100.5f;
             Position.z=(float)(UnityEngine.Random.value-0.5)*90f+50f;
             var qq=GameObject.Instantiate(RabbitObj,Position,Quaternion.identity);
             qq.name=("Rabbit"+i.ToString());
             RabbitObject.Add(qq);
         }
 
 

I have been using the Destroy function but it does not have the desired outcome. It deletes one object but when I try delete another object it returns the following error:

"MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it."

I think I understand why it is saying that (as it has deleted the "RabbitObj" GameObject); I just dont know how to delete just one of the cloned game objects I have instantiated.

I would really appreciate any help :)

Comment
Add comment · Show 8
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 Landern · Mar 04, 2013 at 03:47 PM 0
Share

Are you destroying(RobbitObj) and then later at some point deleting everything in the list/collection in RabbitObject?

avatar image RoloJo · Mar 04, 2013 at 03:59 PM 0
Share

I dont really want to destroy RabbitObj. I want to firstly remove a specific clone of RabbitObj (which was created in line 9). For example I would like to destroy the 13th instantiated clone. Along with destroying clone number 13 I would like to remove it from the ArrayList "RabbitObject".

avatar image dorpeleg · Mar 04, 2013 at 04:16 PM 0
Share

If you could show your destroy code, that could help us help you :)

avatar image robertbu · Mar 04, 2013 at 04:20 PM 1
Share

Something like:

 var qq : GameObject = GameObject.Find("Rabit13");
 if (qq != null) {
    RoabitObject.Remove(qq);
    Destroy(qq);
 }

If you are going to be doing this a lot, you might consider a dictionary rather than a list and storing string/obj pairs.

avatar image $$anonymous$$ · Mar 04, 2013 at 04:22 PM 0
Share

Don't know how your destroy function is structured, but you may be able to access the arraylist and destroy an object at that certain index of the array. What your code error is returning is that it destroys the main object the clones are derived from. You have to specifically search for your clone in the array.

Can use a int like Destroy(RabbitObject[index].gameObject); Not sure it works, never tried something like that destroying inside arrays, but it should get you on the right track.

avatar image RoloJo · Mar 04, 2013 at 04:28 PM 0
Share

This is my code. The destroy part is down the bottom. The first part is just checking the distance between the controller and the object and checking for the keyboard input of "e".

     Vector3 $$anonymous$$yPos;
     GameObject TestObject;
     GameObject ClosestObject;
     float TestDistance;
     float ClosestDistance;
     int RabbitReference=0;
     float DistanceTimer;
     float InteractionRange=5;
     
     void Update ()
     {
         if(Input.inputString.Equals("e"))
         {
         if (DistanceTimer+0.5f<Time.time)
         {
             $$anonymous$$yPos=trans.transform.position;
             for (int i=0;i<=RabbitObject.Count;i++)
             {
     
                 TestObject=(GameObject)RabbitObject[i];
                 
                 if(Vector3.Distance(TestObject.transform.position,trans.transform.position)<InteractionRange)
                 {
                     if(ClosestObject==null)
                     {
                         ClosestObject=(GameObject)RabbitObject[i];
                         ClosestDistance=Vector3.Distance(ClosestObject.transform.position,trans.transform.position);
                     }
                 
                     TestDistance=Vector3.Distance(TestObject.transform.position,trans.transform.position);
                     ClosestDistance=Vector3.Distance(ClosestObject.transform.position,trans.transform.position);
                 
                 
                     if(TestDistance<ClosestDistance)
                     {
                         ClosestObject=TestObject;
                         RabbitReference=i;
                     }
                     if((ClosestDistance<=2))
                     {
                         print (i);
                         RabbitObject.RemoveAt(RabbitReference);
                         Destroy(ClosestObject.gameObject);
                     }
                     DistanceTimer=Time.time;
                 }    
             }        
         }
     }

Thanks so much for the replies. I will try your suggestion now robertbu.

:)

avatar image robertbu · Mar 04, 2013 at 04:36 PM 1
Share

Acutally now that you've shared the code, you should be able to do something like:

 RabitObject.Remove(CloestObject);
 Destory(CloestObject);
avatar image RoloJo · Mar 04, 2013 at 04:42 PM 0
Share

It works... :D :D

Thanks :D :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by selzero · Mar 04, 2013 at 05:21 PM

Firstly I suggest you make RabbitObject an array list, even better, a list using generics.

 public List<GameObject> RabbitObj;



You will need to add:

 Using System.Collections.Generics; 

to the top of your script.

When you want to destroy item X from the list you can call Destroy(RabbitObj[X]); and then remove the item from the list.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

MissingReferenceException after destroying a prefab? 2 Answers

Destroy all previous clones. 2 Answers

MissingReferenceException error. 1 Answer

Clones dont get removed after exiting play mode 0 Answers

GameObject still destroyed after reloading level. 2 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