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 mangosauce_ · Mar 19, 2021 at 02:50 AM · bug-perhapspoolingobject poolpool

[SOLVED] GameObject pooling is not returning enough objects!

SOLVED:


OOPS! Turns out I was forgetting to clear the list of segments per bolt. This meant that when Bolt A deactivated, it would deactivate Bolt B's segments that had been previously parented to A. This is a lesson to avoid tunnel-visioning on one potential cause of a bug, I guess, since it had nothing at all to do with the object pool.


Preface:


I've got a bit of an issue with my object pooling. I have a lightning bolt object composed of a number of gameObject segments (so that I can control the length of the bolt). It seems that at first, the pooling for these segment gameObjects works like a charm.


alt-text Pictured above: working as intended.


Issue


Eventually, though, it looks like either the pool is not big enough (unlikely as it should grow automatically) or retrieving the same object multiple times. The result is that the bolt object is set active and WAM! It's got missing segments.


alt text Pictured above: example of the issue, with segments missing from the bolts, which points to an error in the pooling.


Code


Below is my code for object pooling. When an object is needed, it gets retrieved using the static GetObject method.

 public static class Pool
         {
             private static Dictionary<GameObject, List<GameObject>> pools = new Dictionary<GameObject, List<GameObject>>();
     
             public static List<GameObject> GetPool(GameObject gameObject)
             {
                 // If pooled object list already exists, return it
                 if (pools.TryGetValue(gameObject, out List<GameObject> list))
                     return list;
     
                 // Otherwise, create a new list of pooled objects and
                 // add it to the pool dictionary, then return
                 list = new List<GameObject>();
                 pools.Add(gameObject, list);
                 return list;
             }
     
             public static GameObject GetObject(GameObject prefab)
             {
                 // Get list of pooled gameObjects
                 List<GameObject> list = GetPool(prefab);
     
                 // Prepare a list for potential null objects that need to be
                 // removed from the pool.
                 List<GameObject> objectsToRemove = new List<GameObject>();
     
                 // Enumerate through list of pooled objects to find inactive
                 // object, then activate and return that instance
                 GameObject gameObject = null;
                 for (int i = 0; i < list.Count; i++)
                 {
                     GameObject o = list[i];
                     if (o && !o.activeSelf)
                     {
                         gameObject = o;
                         gameObject.SetActive(true);
                         break;
                     }
                     else if (!o)
                         objectsToRemove.Add(o);
                 }
     
                 // Remove null objects from the pool
                 objectsToRemove.ForEach(o => list.Remove(o));
     
                 // If no inactive instances were found, add a new instance 
                 if (!gameObject)
                 {
                     gameObject = Object.Instantiate(prefab, Group.Pool);
                     if (!list.Contains(gameObject))
                         list.Add(gameObject);
                 }
     
                 // Return object
                 return gameObject;
             }
         }



Below is my code for the lightning bolt creation.

         public static Bolt Strike(Vector3 start, Vector2 direction)
         {
             // Get bolt parent prefab
             GameObject prefab;
             try
             {
                 prefab = Assets.prefabs["Bolt"];
             }
             catch
             {
                 throw new NullReferenceException("Prefab 'Bolt' not found in asset dictionary!");
             }
             
             // Instantiate bolt parent
             GameObject gameObject = Pool.GetObject(prefab);
             gameObject.transform.position = start;
 
             // Rotate bolt parent to attack angle
             float angle = direction.ToAngle();
             Quaternion rotation = Quaternion.identity;
             Vector3 eulerAngles = rotation.eulerAngles;
             eulerAngles.z = angle;
             rotation.eulerAngles = eulerAngles;
             gameObject.transform.rotation = rotation;
 
             // Get the maximum length of the bolt
             Bolt bolt = gameObject.GetComponent<Bolt>();
             float maxLength = bolt.maxLength;
             Vector3 end = start + (Vector3) (direction * maxLength);
             
             // Check for collisions with entities
             RaycastHit2D[] hits = Physics2D.RaycastAll(start, direction, maxLength, LayerMask.GetMask("Entities"));
             float boltLength = maxLength;
 
             // If collision, set target point to collision point + 
             // 72 pixels in bolt direction
             foreach (RaycastHit2D hit in hits)
             {
                 if (hit.collider.CompareTag("Player"))
                     continue;
                 float distance = hit.distance + Size;
                 if (distance < boltLength)
                 {
                     end = hit.point + (direction * Size);
                     boltLength = distance;
                 }
             }
 
             // Get bolt segment prefab
             try
             {
                 prefab = Assets.prefabs["Bolt Segment"];
             }
             catch
             {
                 throw new NullReferenceException("Prefab 'Bolt Segment' not found in asset dictionary!");
             }
 
             // Construct bolt out of segments until length is reached
             Transform parent = bolt.transform;
             SpriteRenderer spriteRenderer = null;
             boltLength -= Size;
             int step = 0;
 
             while (boltLength > 0)
             {
                 start += (Vector3) (direction * Size);
                 gameObject = Pool.GetObject(prefab);
 
                 // Set parent to bolt object
                 gameObject.transform.SetParent(parent);
                 gameObject.transform.position = start;
 
                 // Set rotation to bolt angle
                 rotation = Quaternion.identity;
                 eulerAngles = rotation.eulerAngles;
                 eulerAngles.z = angle;
                 rotation.eulerAngles = eulerAngles;
                 gameObject.transform.rotation = rotation;
 
                 // Set sprite and choose whether to invert it
                 spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
                 Sprite sprite = bolt.middleSprites[Random.Range(0, bolt.middleSprites.Length)];
                 spriteRenderer.sprite = sprite;
                 bool flipSprite = (Random.Range(0f, 1f) >= 0.5) ? true : false;
                 spriteRenderer.flipY = flipSprite;
                 spriteRenderer.flipX = flipSprite;
 
                 // Add to 
                 bolt.segments.Add(gameObject);
                 boltLength -= Size;
                 step++;
                 if (step > MAX_STEPS)
                     break;
             }
 
             // Set end sprite
             if (spriteRenderer)
             {
                 spriteRenderer.flipX = false;
                 spriteRenderer.flipY = false;
                 spriteRenderer.sprite = bolt.endSprite;
             }
 
             // Return bolt parent script
             return bolt;
         }



Any help is greatly appreciated! Thank you for reading my silly wall of text.

notworking.gif (176.4 kB)
workingasintended-1.gif (221.0 kB)
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

112 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

Related Questions

I can't Implement the Pool system 1 Answer

enemy pooling 1 Answer

Pooling GameObjects with TextMesh ? 0 Answers

Pooling Issue 0 Answers

How to Use Object Pooling for a 3D Game? 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