• 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
1
Question by SoraMahiro · Jan 19, 2017 at 04:04 AM · c#parent-childdestroygameobject

Destroy parent after all children have been destroyed

Just as the topic suggests, I would like to destroy a parent object after the children have all been destroyed themselves. I want this so that there isn't a flood of empty gameObjects laying around the hierarchy. I tried using transform.childCount to check the number of children an object has at the time it should be destroyed, but it didn't work. Can your guys lend me a suggestion?

Comment
Add comment · Show 2
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 TBruce · Jan 19, 2017 at 04:41 AM 0
Share

Destroying the parent will destroy all children.

avatar image HenryStrattonFW TBruce · Jan 19, 2017 at 08:37 PM 0
Share

I think the concern is that they have a parent object acting as a container for children, the children may get destroyed at arbitrary time (or at least not at the same time) and when all children have been destroyed they want to clean up the container object.

However I'd my assumption is wrong, then you are completely correct, killing the parent, kills the children.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by HenryStrattonFW · Jan 19, 2017 at 08:41 PM

@UnityCoach has provided a good answer, it will do the job under the condition that all, or at least the last child to be destroyed contains a component with this code.

However should your parent contain multiple different objects that do not all contain this script (and you don't want to add N components into your scene one for each child to ensure this code runs) you can instead run this top down instead of bottom up, by which I mean add a single component (or append a script already on the parent) to check in its Update or LateUpdate if its childCount is 0. If it is, then all children regardless of type are destroyed and you are free to then self-destruct the parent object.

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 UnityCoach · Jan 19, 2017 at 10:02 PM 1
Share

I also considered the top-down approach when answering :) I'm not a big fan of putting things in Update unless it really is to be updated every frame though. Putting a component on every child isn't perfect either. It all depends of the hierarchy tree I guess, if you have a few top parents and many "grand-children" or the opposite. It also depends what is destroying the objects, maybe the check can be done there. So if the object self-destruct, it could check to see if it's the last. Last one to leave, please turn off the lights kind of approach. Or, if you like complexity, you may have an event manager to handle all destructions. :)

avatar image HenryStrattonFW UnityCoach · Jan 20, 2017 at 01:54 PM 0
Share

Yeah as you point out both approaches have pros and cons, all depends on the requirements of the use case I suppose. And the preference of the programmer. The destroy point is an good point though, if there is a 3rd party destroying the objects, that could do the checking as well which would provide the benefit of allowing multiple children, without the need to introduce a new update method.

avatar image
2

Answer by UnityCoach · Jan 19, 2017 at 02:36 PM

 private void OnDestroy ()
 {
     if (transform.parent != null) // if object has a parent
     {
         if (transform.childCount <= 1) // if this object is the last child
         {
             Destroy (transform.parent.gameObject, 0.1f); // destroy parent a few frames later
         }
     }
 }
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

301 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 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 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 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

Getting Parent GameObject from Child? 1 Answer

Particles moving past gun. Please help. 1 Answer

Making an object follow another object C# 1 Answer

Help me with this parent-child relation. 2 Answers

Applying a script to two objects not working 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