• 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 bustedkrutch · Jul 25, 2013 at 06:51 PM · destroy-clones

Trouble Destroying Instantiated Prefab in Array

Having trouble Destroying an instantiated Prefab that i'm referencing in an array (or at least that's what I think i'm doing).

Here is the declaration for the array:

 (Outside of any function)
 var numLaunchObject : Rigidbody[];
 
 (Inside Start function)
 numLaunchObject = new Rigidbody[3];

Here is where I'm instantiating the Prefab into an array:

             numLaunchObject[0] = Instantiate(zeroPrefab, numAnchor_ones.position, numAnchor_ones.rotation);
             numLaunchObject[0].name = "ones";
             numLaunchObject[0].transform.parent = numAnchor_ones.transform;

The array is used as a LIFO buffer for these objects and are moved along using this: ([2] copies [1], then [1] copies [0], and [0] is the new guy)

                 numLaunchObject[1].transform.parent = null;
                 numLaunchObject[2] = numLaunchObject[1];
                 numLaunchObject[2].transform.position = numAnchor_hunds.transform.position;
                 numLaunchObject[2].transform.parent = numAnchor_hunds;    // Not sure if I can do this once in Start or if "Destroy"ing it releases the parent ???
                 numLaunchObject[2].name = "hunds";

Then moved again (3 deep buffer):

                 numLaunchObject[0].transform.parent = null;
                 numLaunchObject[1] = numLaunchObject[0];
                 numLaunchObject[1].transform.parent = numAnchor_tens;
                 numLaunchObject[1].transform.position = numAnchor_tens.transform.position;
                 numLaunchObject[1].name = "tens";


My intent is to destroy any objects that are pushed out of the buffer and my attempt looks like this: (below the long winded disclosure)

A bit of disclosure: I know that I'm trying a couple of ways to destroy this thing and it just won't go away. I've also left in a couple of debug statements in case the need arises. I originally try to destroy the object using it's array reference - no error, but still alive.

Then I see if I'm at least "seeing" it using its name "hunds", and this does return a 1 and I see the debug statement "FOUND HUNDS".

Then I try setting a variable straight to it and attempt another destruction. But the debug with the name appears before and after the destroy, as well as the object remaining in the scene.

                 Destroy(numLaunchObject[2]);    // Get rid of this number to make room for the number to its right
                 if(GameObject.Find("hunds")){
                 Debug.Log("FOUND HUNDS");
                 }
                 var go : GameObject = GameObject.Find("hunds").gameObject;
                 Debug.Log("BEFORE Destroy go - name = " + go.name);
                 Destroy(go);
                 Debug.Log("AFTER Destroy go - name = " + go.name);
                 numLaunchObject[2] = null;

Head bleeding profusely from banging, will refrain until blood loss subsides :(

I'm obviously a nuub, and help in this is greatly appreciated. Thank you upfront!

I've thought of a work around but it's so kludgy I'd really dislike having that code in anything I put out (eventually put out :).

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
1
Best Answer

Answer by oddeven · Jul 25, 2013 at 07:42 PM

The line below does not destroy the GameObject, it destroys the RigidBody! You have an array of RigidBody's not GameObjects.

 Destroy(numLaunchObject[2]);
 
 // change to
 
 Destroy(numLaunchObject[2].gameObject);
Comment
Add comment · Show 2 · 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 bustedkrutch · Jul 25, 2013 at 07:46 PM 0
Share

Thank you oddeven.

Will try immediately!

avatar image bustedkrutch · Jul 25, 2013 at 07:52 PM 0
Share

Yeah man,

I would have sworn I tried that one, but Thank you Thank you Thank you.

Off to the next problem - awayyyyy :)

avatar image
0

Answer by Owen-Reynolds · Jul 25, 2013 at 07:19 PM

Looks like the array copy is backwards. To insert into the front, copy 1 to 2, then copy 0 to 1. Then create the new one in slot 0. Otherwise you just copy the new item from 0 into 1 then again into 2. The former gameObjects fromslot 1 and 2 still exist, but you can no longer find them.

But, if you just want a revolving list, use a linked list. It's made for that stuff. In C#:

 LinkedList<Transform> T = new LinkedList<Transform>();
 Transform t1 =...
 T.AddFirst(t1); T.AddFirst(t2); // list is now t2 t1
 T.RemoveLast(); // got rid of t1 from end
 Transform firstItem = T.First.value;

 foreach(Transform tt in T) ...

Think there's at least one other C# version of a linked list with "easier" syntax.

Comment
Add comment · Show 3 · 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 bustedkrutch · Jul 25, 2013 at 07:26 PM 0
Share

Thanks for your reply Owen,

I really appreciate your catching that, however it was really just me pasting in the wrong order. I'll edit above, but the array copy is as you've noted it should be. 2 gets 1, then 1 gets 0, then 0 is a new entry.

I'm trying to hold the line on using the built-in arrays because of the reported efficiency. (although maybe a list is also that (not sure). If all else fails I will look at another type.

The problem that I'm having however is not in the array copying but in destroying the object that is in the array.

Any ideas? (thank you)

avatar image Owen-Reynolds · Jul 25, 2013 at 08:38 PM 0
Share

As you've clearly demonstrated, arrays are not efficient for FIFO lists. Turns out that linked lists are (AddFirst and RemoveLast could be hiding big, slow loops, but they aren't. They're a single step.)

Then, the second rule for writing optimized code is that 1% of your code takes 90% of the time, so don't bother optimizing the rest. You can't squeeze too much speed out of a size-3 list.

avatar image bustedkrutch · Jul 25, 2013 at 08:47 PM 0
Share

Thanks for your reply Owen,

What I think I've clearly demonstrated is that I have a lot to learn about how to reference objects in java script.

Elsewise the code took me about 5 $$anonymous$$s to write, in my book, when I get better I'll bring that down to 3 :)

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

17 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

Related Questions

guitext and texture following gameobject, how to destroy and alternatives? 1 Answer

Missile GameObject being destroyed automatically! 1 Answer

Difference between Destroy and Destroy immediate (In this case). 2 Answers

How to Destroy Individual Instance of an Object 3 Answers

touch and destroy clone object with specific color 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