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 JamesT · Mar 15, 2011 at 03:27 PM · gameobjectdestroychild

Destroying all child objects.

When attempting to destroy an instance of a game object that has multiple child objects, the object remains visible on the screen unless I do the following:

private void ScheduleForDesruction(Rigidbody gameObject, float time)
{
    foreach(Transform  xform in gameObject.transform)
    {
        Destroy(xform.gameObject, time);
    }           
    Destroy(gameObject, time);      
}

Even then, there is an audio source gizmo that remains visible on the Scene view screen. Everything I've found here in the wiki regarding this indicates that calling Destroy on a game object effectively destroys all the children. This doesn't happen in my case. In fact, if I don't iterate through the entire transform array as above, the object remains visible on the screen.

Is there something I need to do differently here? Perhaps when the object is a Rigidbody, Destroying it requires different treatment? ... or when there's an audio source attached, does it have to be deleted individually?

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
2

Answer by efge · Mar 15, 2011 at 03:32 PM

Destroy(gameObject, time); in your script destroys only the component Rigidbody not the game object itself (because your variable gameObject is of type Rigidbody).

Comment
Add comment · Show 5 · 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 JamesT · Mar 15, 2011 at 03:38 PM 0
Share

Thanks. I just checked it out. The following code does work...

Destroy(gameObject.gameObject);

I had assumed a Rigidbody was equivalent to a gameObject in this regard. $$anonymous$$uch appreciated.

avatar image efge · Mar 15, 2011 at 03:40 PM 0
Share

... or you could change your first line to "private void ScheduleForDesruction(GameObject gameObject, float time)"

avatar image Bunny83 · Mar 15, 2011 at 03:44 PM 0
Share

No, gameObject is always the reference to the containing GameObject. If you destroy the GameObject all containing Components and child GameObjects should be destroyed as well.

avatar image Bunny83 · Mar 15, 2011 at 03:47 PM 0
Share

oh, sorry, i just saw that he named the parameter of his function gameObject.... :D. In this case you're absolutely right.

avatar image Bunny83 · Mar 15, 2011 at 03:52 PM 1
Share

It's not good practise to hide inherited variables or properties as it leads to such errors or confusions.

avatar image
1

Answer by Pi_3.14 · Sep 30, 2012 at 01:51 PM

Courtesy of Zerot on the IRC channel:

0,1,2,3,4

loop: index = 0 : ->0,1,2,3,4 Destroy:
contents shift: ->1,2,3,4 index++
because of loop: 1,->2,3,4 index = 1
Destroy: contents shift: 1,->3,4
index++

separate array/list loop: index = 0 :
->0,1,2,3,4 Destroy: no content shift because of separate array: ->0,1,2,3,4
index++ because of loop: 0,->1,2,3,4
index = 1 Destroy: no content shift
because of separate array: 0,->1,2,3,4

Basically you can't remove elements while inside a fast enumeration, otherwise it will miss alternate elements.

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

1 Person is following this question.

avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy Child of Game Object 2 Answers

Destroying childs and Instantiate [C#] 0 Answers

Player Guide (particles) destroy collectable 0 Answers

Access a child from the parent or other gameObject. 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