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 Andrii · Apr 16, 2016 at 05:27 AM · destroy2d-platformerprefabsproceduralpooling

Procedural platformer: add/remove or pooling for platforms?

I'm trying to make a mobile 2d game when player endlessly jumps up onto procedurally generated platforms. I'm new to Unity and I'm worrying that I might be crating some memory leak situation here. Since this type of game it pretty common, I guess this was already solved multiple times. I create new platforms above the screen area each time player jumps higher up. When screen moves up, you can't go back, falling down is death. So what should I do with platforms that are below the screen area and won't be used?

  • Remove them?

  • Deactivate them?

  • Reuse them?

It's not a bullet hell game when there's hundreds of objects being created and destroyed every second. More like 20-30 objects every few seconds. Should I worry about efficiency? Can it clog the memory? Looking for answers from people who have some experience with this kind of games.

UPD: And actually, I'd also wanted to ask same thing about any temporary sprites I put in the scene. Like, when player jumps or lands, there's a small dust cloud animation being added just above the ground and is being removed when it finishes. Shout it be reused too?

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 tanoshimi · Apr 16, 2016 at 07:13 AM

Instantiating objects requires memory allocation. Destroying them releases the memory, but requires garbage collection. This happens automatically, but It's this process of garbage collection that can causes spikes/lags in your game's performance, which is generally undesirable.

Pooling instantiates objects as they are required (or, sometimes, pre-creates a set of objects as the game is initialised), but doesn't destroy them when they are no longer needed - it just deactivates them and returns them to the pool ready to be used again. So your average memory usage is higher using this method, because all objects that have been placed in the pool remain in memory even when they are not being used, but you avoid the spikes when objects are instantiated or destroyed, since deactivating/re-enabling an object from the pool is a trivial operation that requires no memory management.

Pooling requires a little bit more code infrastructure, but there's plenty of good examples on the net. Which is best for your situation? Try both and use the profiler.

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 Andrii · Apr 16, 2016 at 07:20 PM 0
Share

Thanks for the explanation! I don't want to have my game freezing or skipping frames so I'll better implement pooling. But I'll also try just creating objects as an experiment. I need to find out just how bat it gets.

avatar image
1

Answer by AnatolyDream · Apr 16, 2016 at 12:51 PM

"When screen moves up, you can't go back, falling down is death. So what should I do with platforms that are below the screen area and won't be used?"

Why not use them again? Since it's not a slow paced platformer, I would go with object pooling. Plus, if you don't have a lot of platforms that differ from each other too much then you can just reposition those that were already used before.

Something like this pseudo code:

  1. If platform is down behind the screen - deactivate it.

  2. Go through your object pool and check for deactivated platforms.

  3. Use any of them to randomly position it behind the screen above your character.

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 Andrii · Apr 16, 2016 at 07:17 PM 0
Share

This sounds reasonable. I'll go with this solution.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help with simple issue (hopefully) 1 Answer

Trying to destroy a projectile and "teleport" to another area of the screen when it hits a target. 1 Answer

snapping objects at runtime 2 Answers

Pooling object performs the same or worse than instantiate/destroy? 1 Answer

Can't Destroy Instantiated Prefab 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