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
1
Question by nyonge · May 07, 2017 at 08:56 PM · gameobjectperformancedestroyoptimizationdestroygameobject

Optimize huge number of Destroy() calls (cannot use object pooling)

I have levels that are dynamically generated from preset tiles and objects. Each level can contain any number of any gameobjects, so object pooling doesn't work – no two levels will necessarily have the same objects.

When I switch from one level to the next, I destroy the existing level to make way for the new one. However, calling Destroy() seems to take a long time, sometimes almost ten seconds. The profiler says it may be called up to >50,000 times on one frame, as it destroys every object. Even if I just call it once on the container object the rest of level is spawned into.

Instantiating/creating levels is fast (I've optimized it) but I can't think of a way to speed up object destruction. Any ideas?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by UmairEm · May 09, 2017 at 05:51 AM

@nyonge You can deactive the container by using SetActive(false); instead of destroying it. And then run a coroutine to destroy every child object every frame. This might speed things up. By the way it is always good to show a loading screen while swithing between levels.

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
avatar image
1

Answer by Commoble · May 07, 2017 at 09:09 PM

If you need to destroy that many objects, it might be easier just to start a new scene for each level.

Alternatively, If you can deactivate all of your objects in a reasonable amount of time, consider just deactivating them, then giving them to a script that gradually destroys (or re-pools) the objects as time passes (e.g. destroying 100 condemned objects each frame).

Comment
Add comment · Show 1 · 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 nyonge · May 07, 2017 at 09:12 PM 0
Share

Starting a new scene is impractical for my project, but deactivating objects and adding them to a "condemned" list is a great idea. Will try it out, cheers

avatar image
1

Answer by dfeldhaus · May 09, 2017 at 05:52 AM

Some Ideas:

1) For starters, I'd pool whatever I can. So, keep the old level, generate the new level, see what overlaps, and keep it.

2) If your levels are large enough to warrant that many Destroy() calls, you might also want to consider destroying / re-using objects within the scene as you walk around. That way it'll run better anyway. If something's too far away to see or get to quickly, get rid of it and generate it when you get there.

3) There is this thing called Mesh.CombineMeshes. I've never needed to mess with it, but it might be exactly what you need. If you need the whole level at once, and it's all just static, do some texture magic and stick it all into one object, or chunks.

4) If that doesn't work, swapping out the meshes/textures between levels might. If you're using mesh colliders and you have two objects that are essentially the same except for different geometry/material, you can just swap that out.

If nothing else works, use Co-routines and a loading screen. That's pretty standard for a leveled procedural game.

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

106 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

Related Questions

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

Merging together all GameObjects of an Array? 1 Answer

how to save destroyed state of an object 1 Answer

How to assign a color to a game object & destroy it when it collides with another game object of same Color 1 Answer

Destorying gameobjects on a network 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