Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 treehuggernicky · Apr 10, 2017 at 12:43 PM · gameobjectdestroyloophierarchyefficiency

Deleting GameObjects

I am presented with a situation where I need to delete all object under a GameObject.

I am unsure whether

 Destroy(parent)
 parent =  new GameObject();

or

  for (int j = children - 1; j >= 0; j--) {
            GameObject.Destroy(parent.transform.GetChild(j).gameObject);
 }

is more efficient. I think it all boils down to how Destroy is implemented. Does anyone know which is more efficient?

Comment
Add comment · Show 5
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 Oliver-Bogdan · Apr 10, 2017 at 01:05 PM 0
Share

If efficiency is your concern then maybe you shouldn't destroy the objects in the first place. You can rather set them inactive and maybe reparent them to an inactive parent. Use an Object pool and reuse these objects without re-allocating them in the future.

avatar image treehuggernicky · Apr 10, 2017 at 02:45 PM 0
Share

I actually cannot use the same children, but i must recreate them on each frame, possibly less each frame.

avatar image Pengocat treehuggernicky · Apr 10, 2017 at 03:23 PM 1
Share

Destroying something each frame is going to create a lot of garbage. When the garbage colllector kicks in you will get a frame dip. What prevents you from reusing them?

avatar image treehuggernicky Pengocat · Apr 10, 2017 at 04:20 PM 0
Share

I am using a light clustering algorithm which is dependent on camera distance from light coordinates. Lights are being created as children of an entity after clustering occurs. The points clustered on the next frame may change and that is why i must delete all the children. This is actually part of a B Sc project.I can reuse the lights if the camera does not change in distance but for this project I must recompute the whole pipeline in order to show the worst case scenario fps.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Pengocat · Apr 10, 2017 at 02:15 PM

If you destroy a specific GameObject all of its children will be destroyed with it. You do not need to manually destroy individual children.

 Destroy(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 treehuggernicky · Apr 10, 2017 at 02:41 PM 0
Share

so destroying the parent is more efficient, thanks.

avatar image Pengocat treehuggernicky · Apr 10, 2017 at 03:08 PM 1
Share

On a micro level yes and it is also more readable and maintainable.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Best workflow for DontDestroyOnLoad method regarding Unity Editor? 1 Answer

How to destroy all gameobjects active in hierarchy? 1 Answer

"Some objects were not cleaned up when closing the scene" 1 Answer

Destroy all Objects of a Type in Game 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