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 /
  • Help Room /
avatar image
0
Question by CoolCasualCat · Sep 12, 2015 at 09:25 PM · c#beginnerpoolingobject poolobjectpool

Could I get an explanation of object pooling and how to do it?

Sorry for being such a beginner :/. But I am having a hard time understanding how to write an object pooling script in C#. I understand the general concept that the game is to instantiate a fixed quantity of objects at the start and then replace the instantiation and destroying with activating and deactivating. But I just can't successfully write a working script. I just want to make my game smoother with object pooling.

If anyone is willing to explain anything, it would be greatly appreciated!

Comment
Add comment · Show 1
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 iwaldrop · Nov 26, 2015 at 04:35 AM 0
Share

Whether it's difficult or not is trivial, it's a great learning experience. Take what hexagonius mentions below, and maybe expand on it by resetting the pooled object's state when it's released/returned so it's in the same state it would be if it were just spawned when it's needed again (using an interface perhaps).

Things to experiment with: Write an interface for pooled things so anything can be poolable Create extension methods to the interface for common operations (like Spawn and Release) * Create a pool manager that basically maintains several pools of things

Have fun!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Sep 12, 2015 at 10:35 PM

You can create a pool class (not deriving from MonoBehavior) which has the following fields and methods:

  • constructor; taking in the Item to be pooled and an int for how many of them

  • Stack, and List for available and used objects

  • method Item GetItem()

  • method ReturnItem(Item item)

the constructor fills the stack with items and disables them when GetItem is called, remove it from the stack, add it to the list and return it enabled. when ReturnItem is called, check the list for the item, and if found disable it, remove it from the list and add it to the stack.

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
0

Answer by pako · Sep 13, 2015 at 09:43 AM

It can be quite challenging for a beginner to create a pooling script. Maybe you should consider a pooling solution from the Assets store, where you can find decently priced, or even free packages available. Here are the Asset Store search results for "Pool" search term:

https://www.assetstore.unity3d.com/en/#!/search/pool

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting an Exception on Endless Runner 3D game 1 Answer

How to Activate Random GameObjects from a list without affecting position 1 Answer

Object Pooling causing a gap between last and first reused tile in array 0 Answers

Best way to learn how to utilize the Unity API? 2 Answers

What is wrong with my code? C# beginner 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