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 /
avatar image
1
Question by mealone · Mar 22, 2017 at 08:02 AM · scripting problemobjectspawnpooling

hel with object pooling?

Hi everyone!

I have two scripts; one for object pooling and another which pulls the objects from the pool (no pun intended) and creates a grid. the scripts work, and the grid is created as I would like. Where I am stuck however is creating a pool which consists of various different objects and then having the specific object pulled based on a particular condition. The objects in the pool are circles and squares. I have code on the circles which will deactivate the gameobject and change a static int variable called "item" to "2" when tapped with the OnMouseDownAsButton function. I want to create a script where the object taken from the pool will be the circle when the item variable is set to 1, and a square when the item variable is set to two. I am completely stumped as to how to get this done. Can someone assist with this script?

please take a look at the scripts I have generated thus far:

object pooler script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class ObjectPoolItem
 {
     public int amountToPool;
     public GameObject objectToPool;
     public bool shouldExpand;
 }
 
 public class ObjectPooler : MonoBehaviour {
     public static ObjectPooler SharedInstance;
     public List<GameObject> pooledObjects;
     public List<ObjectPoolItem> itemsToPool;
 
     void Awake()
     {
         SharedInstance = this;
     }
 
     // Use this for initialization
     void Start()
     {
         pooledObjects = new List<GameObject>();
         foreach (ObjectPoolItem item in itemsToPool)
         {
             for (int i = 0; i < item.amountToPool; i++)
             {
                 GameObject obj = (GameObject)Instantiate(item.objectToPool);
                 obj.SetActive(false);
                 pooledObjects.Add(obj);
             }
         }
     }
 
     public GameObject GetPooledObject(string tag)
     {
         for (int i = 0; i < pooledObjects.Count; i++)
         {
             if (!pooledObjects[i].activeInHierarchy && pooledObjects[i].tag == tag)
             {
                 return pooledObjects[i];
             }
         }
         foreach (ObjectPoolItem item in itemsToPool)
         {
             if (item.objectToPool.tag == tag)
             {
                 if (item.shouldExpand)
                 {
                     GameObject obj = (GameObject)Instantiate(item.objectToPool);
                     obj.SetActive(false);
                     pooledObjects.Add(obj);
                     return obj;
                 }
             }
         }
         return null;
     }
 }
 


Grid script:

 public List<GameObject> circles = new List<GameObject>();
     public List<Vector3> circles1 = new List<Vector3>();
     public static Vector3 positions = new Vector3();
     public int gridheight = 4;
     public int gridwidth = 4;
     public static bool on = false;
     public bool on1 = false;
     public GameObject self;
 
 
     void Start()
     {
         StartCoroutine(Spawn1());
         gameObject.transform.position = new Vector3(-5.1f, -4.74f, 0);
     }
 
     void Update()
     {
         on1 = on;
         if (statics.playagain == false)
         {
             Destroy(self);
         }
         if (circles1.Count < 16)
         {
             foreach (GameObject a in circles)
             {
                 circles1.Add(a.transform.position);
             }
         }
         foreach (GameObject b in circles)
         {
             if (!b.activeSelf)
             {
                 StartCoroutine(ReSpawn(b));
             }
         }
         }
 
     IEnumerator Spawn1()
     {
         yield return new WaitForSeconds(0.1f);
         for (var y = 0; y < gridheight; y++)
         {
             for (var x = 0; x < gridwidth; x++)
             {
                 GameObject g = ObjectPooler.SharedInstance.GetPooledObject("black1");
                 if (g != null)
                 {
                     g.transform.position = new Vector3(x - 1.47f, y - 1.41f, 0);
                     g.transform.parent = gameObject.transform;
                     g.SetActive(true);
                 }
                 circles.Add(g);
             }
         }
     }
 
 
     IEnumerator ReSpawn(GameObject target)
     {
         yield return new WaitForSeconds(5f);
         target.SetActive(true);
     }


can anyone assist with this? It will be greatly appreciated!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · Mar 22, 2017 at 08:54 AM

You could use a dictionary with a generic value where the key is your tag. It will then retrieve the generic value (which can be whatever kind of object you like) by the given tag. That way you can put in your object pool whatever you want.

Something like:

 Dictionary<string, TValue> pooledObjects = new Dictionary<string, TValue>();
 pooledObjects.Add("circel", circelObject); //circelObject can be anything
 pooledObjects.Add("square", squareObject); //squareObject can be anything

https://www.dotnetperls.com/dictionary

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 Bunny83 · Mar 22, 2017 at 09:40 AM

Well, a single pool is ment to represent cached versions of a single item. It doesn't make much sense to put different types into a single pool. You either have to:

  • Make your pool class "instantiable" (i.e. no static stuff) and create seperate instances of that class, one for each object type.

  • Change your pool class to actually contain multiple pools

It seems that your "ObjectPoolItem" is actually what represents a single type of object. So simply implement your pool in there. You also might want to use two seperate lists for each pool. One active list and one pool list. When you request an object from the pool you simply grab one object from the pool, remove it from the pool list and move it to your active list. Just keep going until you run out of objects in your pool list. If the pool list is empty you do a single run through your "active list" and move the inactive objects back to the pool list.

Here's an example:

 [System.Serializable]
 public class ObjectPoolItem
 {
     public int amountToPool;
     public GameObject objectToPool;
     public bool shouldExpand;
     [NonSerialized]
     public Stack<GameObject> pool = new Stack<GameObject>();
     [NonSerialized]
     public List<GameObject> active = new List<GameObject>();
     public GameObject Get()
     {
         if (pool.Count > 0)
         {
             var obj = pool.Pop();
             active.Add(obj);
             return obj;
         }
         for (int i = active.Count - 1; i >= 0; i--)
         {
             if (!active[i].activeInHierarchy)
             {
                 pool.Push(active[i]);
                 active.RemoveAt(i);
             }
         }
         if (pool.Count > 0)
         {
             var obj = pool.Pop();
             active.Add(obj);
             return obj;
         }
         /* no objects left instantiate a new object or return null*/
         if (shouldExpand)
         {
             var obj = UnityEngine.Object.Instantiate(objectToPool);
             active.Add(obj);
             return obj;
         }
         return null;
     }
     public void Init()
     {
         for (int i = 0; i < amountToPool; i++)
         {
             pool.Push(UnityEngine.Object.Instantiate(objectToPool));
             pool.Peek().SetActive(false);
         }
     }
 
 }
 
 public class ObjectPooler : MonoBehaviour
 {
     public static ObjectPooler SharedInstance;
     public List<ObjectPoolItem> itemsToPool;
     private Dictionary<string, ObjectPoolItem> m_Lookup = new Dictionary<string, ObjectPoolItem>();
 
     void Awake()
     {
         SharedInstance = this;
     }
 
     // Use this for initialization
     void Start()
     {
         m_Lookup.Clear();
         foreach (ObjectPoolItem item in itemsToPool)
         {
             item.Init();
             m_Lookup.Add(item.objectToPool.tag, item);
         }
     }
 
     public GameObject GetPooledObject(string tag)
     {
         ObjectPoolItem item;
         if (m_Lookup.TryGetValue(tag, out item))
         {
             return item.Get();
         }
         Debug.LogWarning("No pooled object with tag: " + tag);
         return null;
     }
 }

Each "ObjectPoolItem" basically is a seperate pool. The "ObjectPooler" class just manages multiple ObjectPoolItems and uses a dictionary to speed up the tag lookup. Since each ObjectPoolItem only contains a single type of object it's easier to maintain the 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

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

Related Questions

How to Use Object Pooling for a 3D Autorunner? As Opposed to Instantiate? 0 Answers

Issues on spawning the enemy? 0 Answers

Spawn an object with additional parameter 1 Answer

object not spawning but no error message 2 Answers

Flip an object based on array value in other object 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