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 ina · Dec 18, 2011 at 04:38 AM · instantiateperformanceoptimizationruntime

GameObject-Pool to avoid performance issues in Instantiating many objects?

Can someone explain how GameObject-Pool works in avoiding performance issues using Instantiate for many object?

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 Fattie · May 04, 2013 at 07:09 AM 0
Share

Ina, you haven't seen my immense explanation ?

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

4 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by aldonaletto · Dec 18, 2011 at 12:56 PM

Instantiate and Destroy are heavy operations: the system must allocate a lot of memory blocks to instantiate a single object, and free these blocks when destroying the object. This takes time, and also causes more frequent garbage collections (the internal frame-rate-killer operation that "cleans the house", "pulling" active memory blocks to the beginning of the heap).
When using some kind of GameObject pool, frequently used objects like missiles, projectiles, decals etc. may be pre-instantiated at the beginning, deactivated and stored in the pool. When launching a missile, for instance, you actually activate a pre-created missile, move it to the spawn position and launch it. When the missile explodes or disappear in the distance, it's deactivated instead of destroyed, returning to the pool for future use. This save lots of memory manager operations, improving the performance - and matching the ecological recycling hysteria of these days too!
But things may not be so easy: a generic GameObject Pool implementation may be more expensive in some cases than the Instantiate/Destroy alternative. Take a look at this article from Von Lehe: it contains a generic GameObject Pool implementation with code, project and examples, as well as several user comments pointing pros and cons.

Comment
Add comment · Show 5 · 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 Hamesh81 · May 04, 2013 at 07:07 AM 0
Share

This is really helpful, wish information like this was in the docs so it was easier to find. Thanks

avatar image SinisterRainbow · Oct 14, 2013 at 09:05 PM 0
Share

Link is dead for the article.

avatar image Fattie · Oct 14, 2013 at 09:29 PM 0
Share

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

avatar image Shark-Boy · Aug 10, 2014 at 08:57 PM 0
Share

This is a great answer. It explains object pools and how they work very well. If you need a generic object pooling script take a look at this question and answer (yes I kind of answered my own question)

avatar image MisterFlufyyy · Aug 31, 2014 at 06:42 AM 0
Share

If you want to get really wild, when you need orc #21 pull up another block of orcs[20] but step carefully or you can end up with n many of these blocks with one active orc apiece in them in a worst case scenario.

avatar image
5

Answer by Owen-Reynolds · Dec 18, 2011 at 05:43 PM

Here's a really simple example, for enemies. Spawn the max amount (20) all at once, but inactive:

 // make an array of 20 enemies:
 Transform[] E; 

 // In Start:
 E = = new Transform[20];
 for(int i=0;i<20;i++) {
   E[i] = Instantiate(orc, Vector3.zero, Quaternion.identity) as Transform;
   E[i].active = false;
 }

To "spawn" one in the game, grab an inactive one. If it has lots of variables, have to reset them all. A drawback is you can't spawn more than 20

 // search list for unused orc:
 int orcNum = -1;
 for(int i=0; i<20;i++)
   if(E[i].active=false) { orcNum=i; break; }
 // found one (if we couldn't find unused orc, no spawn):
 if(orcNum>=0) { 
   // activate this orc, move it to spawn point:
   E[i].active=true;
   E[i].position = newOrcSpot;
   // Hand-reset all orc stats: (a real instatiate would auto-do this) 
   E[i].GetComponent<orcScript>().hitPoints = 100;
     ....
 }

To destroy, turn it inactive again. replace Destroy(gameObject); with gameObject.active=false;.

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 Sanosake1 · Jan 23, 2012 at 04:45 AM 0
Share

This is exactly what i was looking for!

avatar image theAfrican · Jun 11, 2013 at 10:31 PM 0
Share

great answer, was having this problem with launching missiles that had smoke trails from a helicopter. frame rate would fall. thx again

avatar image
0

Answer by AshwaniKumar · Jan 22, 2015 at 07:54 AM

Hello:

I have written a post on how to create an Object pool and use it in the game. Check it here.. Demo for this can be seen here.

Thanks.

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 SweetSodaGames · Feb 28, 2021 at 11:32 AM

Thank you for this topic. I really wanted to reach a question like this.

Because my game is sometimes spawn over 400-500 enemy in 1 second. And when starts the instantiate, fps is goes down. I created a Pool after reading this topic.

 public class Pool : Singleton<Pool>
 {
     public GameObject[] pool;
 }

And Stacked inside pool over 1000 object; sprite=null , all scripts.enabled = false.

when they die, teleport to the pool location. Theys out the map, and return to being nothing. Just a spawner script give them patrol points and who they are like "EnemyTypes.Enemy1"

My fps was droped form 60 to 23 24 without pool. Now after poolisation not even drop under 60.

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

15 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

Related Questions

Optimize mesh for runtime performance 0 Answers

Need optimizing tips for mobile third person shooting game. 2 Answers

Is Instantiating bullets/many objects always bad for performance? 1 Answer

Should i bake the skinned mesh? 1 Answer

Why did my render time increase after lowering the vertex count? 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