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
0
Question by Codelyy · Aug 03, 2016 at 04:06 AM · c#gameobjectobject pool

Object Pooling Help

I've just started creating a Object Pooler for my game to be able to recycle multiple things such as explosion effects and enemies. I followed a tutorial online, this one: https://www.youtube.com/watch?v=dm_r8KxeS7Y and ended up with this script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ObjectPooler : MonoBehaviour {
 
     public static ObjectPooler current;
 
     public GameObject pooledObject;
     public int pooledAmount;
 
     List<GameObject> pooledObjects;
 
     void Start()
     {
         pooledObjects = new List<GameObject>();
         for(int i = 0; i < pooledAmount; i++)
         {
             GameObject obj = (GameObject)Instantiate(pooledObject);
             pooledObjects.Add(obj);
             obj.SetActive(false);
         }
     }
   
     public GameObject InstantiatePooledObject()
     {
         for(int i = 0; i < pooledObjects.Count; i++)
         {
             if(!pooledObjects[i].activeInHierarchy)
             {
                 return pooledObjects[i];
             }
         }
         GameObject obj = Instantiate(pooledObject) as GameObject;
         obj.SetActive(false);
         pooledObjects.Add(obj);
         return obj;
     }
 
     public void DestroyPooledObject(GameObject obj, float time)
     {
         StartCoroutine(DestoyingPooledObject(obj, time));
     }
 
     IEnumerator DestoyingPooledObject(GameObject obj, float time)
     {
         yield return new WaitForSeconds(time);
         pooledObjects.Add(obj);
         obj.SetActive(false);
         yield break;
     }
 }

I did change a couple of things to suit my needs including adding a method that will disable a pooled object after a certain amount of time. Then I simply attached it to a empty GameObject and filled in the empty variables. This Object Pooler works fine but the main big problem with it is that it only pools one object and when I want to use the Object Pooler for enemies, it won't work the way it is since I'm already using it for explosion effects.

I was wondering if someone could help me get this working for multiple GameObjects? I can't really think of a way to do it. And sorry if this is an amateur question.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KuR5 · Aug 03, 2016 at 05:30 AM

Hey,

What you did is just fine and you can use it for your requirement as below.

Use this script multiple times for multiple objects like enemy and explosion effect. Further you can not use Singleton (instance named current). You can reference all of them at another manager class and by using references from that you can access all pools.

Ex.

Lets Make gameobjects named EnemyPool and ExplosionPool and assign this script with respected values to each of them. Now you can access EnemyPool using GameObject.Find("EnemyPool").GetComponent<ObjectPooler>() and ExplosionPool using GameObject.Find("ExplosionPool").GetComponent<ObjectPooler>()

I hope this will help you, and free to ask further help you need.

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 Codelyy · Aug 03, 2016 at 01:51 PM 0
Share

I don't know why I didn't think of doing that :P. Thanks :)

Another issue I have though is in my game, I have rounds and I'm spawning different enemies each round which I of course want to be recycled but I don't see a way of setting up an EnemyPool that will work for lots of different enemies

avatar image Codelyy · Aug 03, 2016 at 02:58 PM 0
Share

It's fine, I've figured out a way :)

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

204 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# GameObjects Instantiate Into Each Other Issue 1 Answer

How to run a function in a GO that have DontDestroyOnLoad 1 Answer

How to Check if a component Exists on a Gameobject. 3 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