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
0
Question by Rick74 · Mar 11, 2014 at 02:29 PM · javascripttrackingpooling

Better way to keep track of pooled objects?

So I've finally managed to wrap my head around pooling, and I got it to work now successfully. However, I feel there has to be a cleaner method of keeping track of objects that my enemy spawner can use. (ones that are obviously currently inactive off screen)

Right now, inside function update I'm calling this check every frame.

 currentEnemy01 = enemyPrefabs01[currentEnemy01Tracker];
     if (currentEnemy01.activeSelf)
     {
         currentEnemy01Tracker ++;
     }
     if (currentEnemy01Tracker >= enemyPrefabs01.Length)
     {
         currentEnemy01Tracker = 0;
     }

currentEnemy01Tracker is obviously an int variable I use to keep track of which item I can use next. But the thought of running this check every frame seems a bit on the overkill side.

I've read this answer about a half dozen times now and I still don't fully understand it which is why I ended up just making my own based off of a youtube tutorial I found.

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

What I have right now is working great. I just need a more efficient way of finding the next available object in the pool, which could be any of them because enemies do not typically tend to die in a uniformed order.

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
2
Best Answer

Answer by whydoidoit · Mar 11, 2014 at 02:56 PM

You want to use a Stack object onto which you push your instances and Pop them off when you need them.

In javascript you would do this (perhaps in a script call PoolManager):

   import System.Collections.Generic;

   ...

  public static pool = new Stack.<Transform>();
  public prefab : Transform;

  for(var i = 0; i < 10; i++)  {
      var instance : Transform = Instantiate(prefab);
      instance.gameObject.SetActive(false);
      pool.Push(instance);
  }

Now you can check if you have any:

    if(PoolManager.pool.Count > 0) { ...

And get one:

   var obj = PoolManager.pool.Pop();
   obj.gameObject.SetActive(true);

When you disable you do this:

   someObject.gameObject.SetActive(false);
   PoolManager.pool.Push(someObject.transform);

You could make it even cleverer by having a Dictionary of stacks like this:

     public static pool = new Dictionary.<Transform, Stack.<Transform>>();

Then you could add and retrieve prefabs using the reference to the prefab:

   function PoolThis(prefab : Transform) {
      if(!pool.ContainsKey(prefab)) {
         pool[prefab] = new Stack.<Transform>();
      }
      for(var i = 0; i < 10; i++)  {
          var instance : Transform = Instantiate(prefab);
          instance.gameObject.SetActive(false);
          pool[prefab].Push(instance);
      }

   }

And check your pools of the different prefabs like this:

    if(PoolManager.pool[somePrefab].Count > 0) {...
Comment
Add comment · Show 10 · 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 Rick74 · Mar 11, 2014 at 03:29 PM 0
Share

Thanks for the info!

avatar image whydoidoit · Mar 13, 2014 at 01:31 AM 1
Share

Stack is a .NET thing so mostly doc'd in Cs but works fine in js

avatar image Eric5h5 · Mar 13, 2014 at 02:29 AM 1
Share

Don't use the Unity JS Array class; it's slow and untyped.

avatar image whydoidoit · Mar 13, 2014 at 01:19 PM 1
Share

It have something on the enemy that references it's prefab then you just do:

  Pool$$anonymous$$anager.pool[thisEnemy.prefab].Push(thisEnemy.transform);
avatar image whydoidoit · Mar 13, 2014 at 01:34 PM 1
Share

I mean create a script that has a variable which you set to be the prefab you used to create it.

    public Transform prefab;
Show more comments

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

22 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Grabbing an object from array 0 Answers

Using an object pool with Javascript. 1 Answer

Objects in object pool not reactivating. 1 Answer

Object Pooling With JavaScrip 0 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