Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
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 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);
Show more comments

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.

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

Destroyed clones freezing but not disappearing? 1 Answer

The object of type 'Transform' has been destroyed but you are still trying to access it. 0 Answers

Ignoring collisions with clones 3 Answers

Tagged Clones not destroyed by script 1 Answer

cant destroy other object 1 Answer


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