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 /
This question was closed May 25, 2013 at 05:50 PM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by 1337GameDev · May 25, 2013 at 04:29 AM · gameobjectinstantiatemanagerpoolavoid

What's the easiest way to create an Instance Manager as to avoid calling Instantiate during game runtime?

I am programming for mobile, and am told that Instantiate is an expensive operation. I also was told to avoid this, you must use a pooling system, where objects that would be instantiated are created at game start and added to an available pool off screen (where they are disabled and awake, start, etc are not called) and then activated and moved when they are needed, and removed from the available pool. Then when the gameobject has served its purpose, it is deactivated and moved back into the pool, off screen.

What is the best way to implement this, in the respect of efficiency and complexity to use in game code?

Comment
Add comment · Show 3
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 robertbu · May 25, 2013 at 05:40 AM 0
Share

See:

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

avatar image Fattie · May 25, 2013 at 09:02 AM 0
Share

this is just called having a "pool" -- if you invented this yourself, well done because it's a basic in video games !!! well done

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

pls vote up my essay there as I desperately need moar points

avatar image Owen-Reynolds · May 25, 2013 at 04:13 PM 0
Share

"I also was told [...] you must use a pooling system." I suspect that if you dig, than that guy heard it from a friend of his cousin. I Instantiate on an iPad like ... well, someone who instantiates a lot ... and it's fine. Threw in an object pool (for those darn non-autodestructing shuriken PEs) and didn't see a difference.

If you spawn and destroy a lot (20+ items/second?) and that's giving the game hiccups, then, sure, managing your own object pool might help. Pools are an operating systems concept, if you want to look up the source material on them.

1 Reply

  • Sort: 
avatar image
0

Answer by Diet-Chugg · May 25, 2013 at 07:57 AM

Here's a script that I used for this which has a couple more features than you need and

using UnityEngine; using System.Collections; using System.Collections.Generic;

 [System.Serializable]
 public class GameObjectPool : MonoBehaviour
 {
     public GameObject[] pool;
     
     public GameObject Drop()
     {
         foreach(GameObject drop in pool)
         {
             if(!drop.activeInHierarchy)
             {
                 drop.SetActive(true);
                 return drop;
             }
         }
         return null;
     }
     
     public GameObject[] GetActiveInPool()
     {
         List<GameObject> actives = new List<GameObject>();
         foreach(GameObject drop in pool)
         {
             if(drop.activeInHierarchy)
                 actives.Add(drop);
         }
         return actives.ToArray();
     }
     
     public GameObject[] GetDroppables()
     {
         List<GameObject> droppables = new List<GameObject>();
         foreach(GameObject drop in pool)
         {
             if(!drop.activeInHierarchy)
                 droppables.Add(drop);
         }
         return droppables.ToArray();
     }
 }

Just fill the array with in-active objects. In other scrips call the Drop() function on this component which will give you an object from the pool that's inactive (if there is one) the game object you get will still be where it originally was but you can move that object wherever you want by changing it's transform.position. It's proved to be plenty efficient for me. Just check to make sure the object you get from Drop() isn't null.

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

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Trash Collection For Instantiated Game Object 1 Answer

Get components of multiple instantiated objects? 2 Answers

Can I use AddComponent to add (this.script) to an object? 1 Answer

Object Instantiate 1 Answer

Rotating a Vector3 in Instantiation 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