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 mike1233 · Dec 29, 2016 at 10:30 PM · pooling

Please help my pool script doesnt work.

this is 2d game
I attached this script to many objects trying to see if it works on any and it doesn't

under the prefab gameobjects I used another gameobject but nothing happens do I need another instantiate script to make this script work?

please help

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ObjectPool : MonoBehaviour
 {
     public static ObjectPool current;            //A public static reference to itself (make's it visible to other objects without a reference)
     public GameObject[] prefabs;                //Collection of prefabs to be poooled
     public List<GameObject>[] pooledObjects;    //The actual collection of pooled objects
     public int[] amountToBuffer;                //The amount to pool of each object. This is optional
     public int defaultBufferAmount = 10;        //Default pooled amount if no amount abaove is supplied
     public bool canGrow = true;                    //Whether or not the pool can grow. Should be off for final builds
 
     GameObject containerObject;                    //A parent object for pooled objects to be nested under. Keeps the hierarchy clean
 
 
     void Awake ()
     {
         //Ensure that there is only one object pool
         if (current == null)
             current = this;
         else
             Destroy(gameObject);
         //Create new container
         containerObject = new GameObject("ObjectPool");
         //Create new list for objects
         pooledObjects = new List<GameObject>[prefabs.Length];
         
         int index = 0;
         //For each prefab to be pooled...
         foreach ( GameObject objectPrefab in prefabs )
         {
             //...create a new array for the objects then...
             pooledObjects[index] = new List<GameObject>(); 
             //...determine the amount to be created then...
             int bufferAmount;
             if(index < amountToBuffer.Length) 
                 bufferAmount = amountToBuffer[index];
             else
                 bufferAmount = defaultBufferAmount;
             //...loop the correct number of times and in each iteration...
             for ( int i = 0; i < bufferAmount; i++)
             {
                 //...create the object...
                 GameObject obj = (GameObject)Instantiate(objectPrefab);
                 //...give it a name...
                 obj.name = objectPrefab.name;
                 //...and add it to the pool.
                 PoolObject(obj);
             }
             //Go to the next prefab in the collection
             index++;
         }
     }
 
     public GameObject GetObject( GameObject objectType)
     {
         //Loop through the collection of prefabs...
         for(int i=0; i<prefabs.Length; i++)
         {
             //...to find the one of the correct type
             GameObject prefab = prefabs[i];
             if(prefab.name == objectType.name)
             {
                 //If there are any left in the pool...
                 if(pooledObjects[i].Count > 0)
                 {
                     //...get it...
                     GameObject pooledObject = pooledObjects[i][0];
                     //...remove it from the pool...
                     pooledObjects[i].RemoveAt(0);
                     //...remove its parent...
                     pooledObject.transform.parent = null;
                     //...and return it
                     return pooledObject;
                     
                 }
                 //Otherwise, if the pool is allowed to grow...
                 else if(canGrow) 
                 {
                     //...write it to the log (so we know to adjust our values...
                     Debug.Log("pool grew when requesting: " + objectType + ". consider expanding default size.");
                     //...create a new one...
                     GameObject obj = Instantiate(prefabs[i]) as GameObject;
                     //...give it a name...
                     obj.name = prefabs[i].name;
                     //...and return it.
                     return obj;
                 }
                 //If we found the correct collection but it is empty and can't grow, break out of the loop
                 break;
                 
             }
         }
 
         return null;
     }
 
     public void PoolObject ( GameObject obj )
     {
         //Find the correct pool for the object to go in to
         for ( int i=0; i<prefabs.Length; i++)
         {
             if(prefabs[i].name == obj.name)
             {
                 //Deactivate it...
                 obj.SetActive(false);
                 //..parent it to the container...
                 obj.transform.parent = containerObject.transform;
                 //...and add it back to the pool
                 pooledObjects[i].Add(obj);
                 return;
             }
         }
     }
     
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

84 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object Pooling Help 0 Answers

unity threadpool or socket bug? 0 Answers

Questions about Object pooling 0 Answers

Will GameObject remove from List.RemoveAt() destroy automatically? 1 Answer

[C#] Creating falling coins 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