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
1
Question by TheValar · Nov 06, 2013 at 01:58 PM · 2dpoolinginfiniterunneroptimize

Object Pooling: deactivating vs. moving

Preface (skip if you want): I'm currently working on a 2d infinite runner that spawns "ground piecs" randomly from a list. Currently I Instantiate prefabs on the right side of the viewport and destroy them when they go off the screen to the left. I'm about to start optimizing this by implementing object pooling.

The Actual Question: Anyway my question is when I want to "delete" the object (move it back into the pool) is it better to use GameObject.SetActive(false) or to disable the components of the object and move it out of view?

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 slayer29179 · Nov 06, 2013 at 02:17 PM 0
Share

This may sound out of context but why don't you just use Destroy(GameObject); then use Instantiate to spawn the items from a gameobject array? :)

avatar image TheValar · Nov 06, 2013 at 02:24 PM 1
Share

That's what I'm doing currently but it's recently come to my attention that pooling is more performant.

When objects get created and deleted it causes the garbage collector to trigger frequently which can cause stutters every few seconds (which is what I'm experiencing).

avatar image TonyLi · Nov 06, 2013 at 02:40 PM 0
Share

Why wouldn't you just deactivate the GameObject? It automatically disables the components (even if you add or remove components dynamically), and it's a little less processing that Unity has to do every frame/timestep. I don't see any advantages to disabling the components.

avatar image TheValar · Nov 06, 2013 at 03:28 PM 0
Share

Well that's the question isn't it. I assume that deactivating the GameObject would be better but in places where pooling has been discussed i.e. http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html it seems like some choose to move the transform to an offscreen location. I'm curious about the differences.

avatar image dorpeleg · Nov 07, 2013 at 08:47 AM 1
Share

If you only move the object off screen, the object will not be rendered but its scripts will still work.

If you disable it, and will not be rendered and its scripts will not work.

So your choice should be based on "do I need the scripts running after i'm 'removing' the object or not".

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by flamy · Nov 07, 2013 at 09:00 AM

It is your choice and depends on how you build the scene and stuff.

If you have a script with some code in update() function, then it is better to disable the whole game object. This is because even when out of focus UPDATE method runs on all objects

If it is simple gameobject without any scripts attached or some simple script without update (pick up objects would have only OnTriggerEnter()), just move it out of screen, that would do.

DONT go around disabling individual components, because you would have to have maintain lot of references.

My personal suggestion is to disable the object itslef, it is simple to do ;)

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

19 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

Related Questions

2D Vertical scrolling infinite background 1 Answer

What's the most efficient method of implementing an infinite parallax background? 0 Answers

Infinite Runner Tutorial - Destroyer Problem 2 Answers

cannot respawn pickup objects infinite runner / flyer 3 Answers

Programming Movement Momentum 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