Home Unity

Pooling System

Greg DAlessandro
polycounter lvl 11
Offline / Send Message
Greg DAlessandro polycounter lvl 11
I can't figure out why I am only able to spawn 1 object, it correctly spawns 10 clones, but I am only able to activate one of them at any given time. Does anyone know why this is?
Script A:
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class Pool : MonoBehaviour
  5. {
  6. public GameObject[] objects;
  7. public int[] number;
  8.  
  9. public List<GameObject>[] pool;
  10.  
  11. // Use this for initialization
  12. void Start ()
  13. {
  14. instantiate ();
  15. }
  16.  
  17. // Instantiate all the objects
  18. void instantiate()
  19. {
  20. GameObject temp;
  21. pool = new List<GameObject>[ objects.Length ];
  22. for ( int count = 0; count < objects.Length; count++ )
  23. {
  24. pool[ count ] = new List<GameObject>();
  25. for( int num = 0; num < number[ count ]; num++ )
  26. {
  27. temp = ( GameObject ) Instantiate( objects[ count ] );
  28. temp.transform.parent = this.transform;
  29. pool[ count ].Add ( temp );
  30. }
  31. }
  32. }
  33.  
  34. public GameObject activate( int id )
  35. {
  36. for( int count = 0; count < pool[ id ].Count; count++ )
  37. {
  38. if( !pool[ id ][ count ].activeSelf );
  39. {
  40. pool[ id ][ count ].SetActive ( true );
  41. return pool[ id ][ count ];
  42. }
  43. }
  44. pool [ id ].Add ( ( GameObject ) Instantiate ( objects[ id ] ) );
  45. pool[ id ][ pool[ id ].Count - 1 ].transform.parent = this.transform;
  46. return pool[ id ][ pool[ id ].Count - 1 ];
  47. }
  48.  
  49. public GameObject activate( int id, Vector3 position, Quaternion rotation )
  50. {
  51. for( int count = 0; count < pool[ id ].Count; count++ )
  52. {
  53. if( !pool[ id ][ count ].activeSelf );
  54. {
  55. pool[ id ][ count ].SetActive ( true );
  56. pool[ id ][ count ].transform.position = position;
  57. pool[ id ][ count ].transform.rotation = rotation;
  58. return pool[ id ][ count ];
  59. }
  60. }
  61. //I may need to fix this so it doesn't spawn at the last known location
  62. pool[ id ].Add( ( GameObject ) Instantiate( objects[ id ] ) );
  63. pool[ id ][ pool[ id ].Count - 1 ].transform.position = position;
  64. pool[ id ][ pool[ id ].Count - 1 ].transform.rotation = rotation;
  65. pool[ id ][ pool[ id ].Count - 1 ].transform.parent = this.transform;
  66. return pool[ id ][ pool[ id ].Count - 1 ];
  67. }
  68.  
  69. public void deActivate( GameObject deActivateObject )
  70. {
  71. deActivateObject.SetActive ( false );
  72. }
  73. }

Script B: just to test the spawning
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Shoot : MonoBehaviour {
  5.  
  6. public Pool pool;
  7.  
  8. // Use this for initialization
  9. void Start ()
  10. {
  11.  
  12. }
  13.  
  14. // Update is called once per frame
  15. void Update ()
  16. {
  17. if( Input.GetKeyDown( KeyCode.C ) )
  18. {
  19. pool.activate( 0, new Vector3( 1.3f, 0f, 0.1f), Quaternion.identity );
  20. }
  21. // If I have a second Element I want to add
  22. // else if( Input.GetKeyDown( KeyCode.S ) )
  23. // {
  24. // pool.activate( 1, , new Vector3( 1.3f, 0f, 0.1f), Quaternion.identity );
  25. // }
  26. }
  27. }

Script C: attached to the prefab object (the sphere in the PoolingObjects folder)
using UnityEngine;
using System.Collections;

public class PoolMember : MonoBehaviour
{

    public float life;

    float timeToDie;

    // Use this for initialization
    void OnEnable ()
    {
        timeToDie = life + Time.time;
    }

    // Update is called once per frame
    void Update ()
    {
        if( Time.time > timeToDie )
        {
            gameObject.SetActive ( false );
        }
    }
}

ItemPoolQuestion_zpskfcc9uio.jpg
The Sphere in PoolingObjects is the prefab being instantiated (Element 0 ).
For some reason only one clone is able to become active when I press the C key.

Replies

  • SanderDL
    Offline / Send Message
    SanderDL polycounter lvl 7
    When you are running the activate function by pressing C you are always putting in "0" as the ID which is just the first object from the list. Instead you could replace 0 with a variable that you always change afterwards. Something like this:

    1. if( Input.GetKeyDown( KeyCode.C ) )
    2. {
    3. pool.activate( i , new Vector3( 1.3f, 0f, 0.1f), Quaternion.identity );
    4. i++;
    5. }

    In my example you will soon run out of objects on the list though. So you could check if it has reached the end of the list and then just reset the varianle to zero:
    1. if( Input.GetKeyDown( KeyCode.C ) )
    2. {
    3. pool.activate( i , new Vector3( 1.3f, 0f, 0.1f), Quaternion.identity );
    4. i++;
    5. if (i > pool.Lenght)
    6. i = 0;
    7. }
Sign In or Register to comment.