Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object Pooling

Discussion in 'Scripting' started by Yourname, Apr 20, 2015.

  1. Yourname

    Yourname

    Joined:
    May 21, 2013
    Posts:
    15
    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:
    Code (CSharp):
    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.  
    23.         for ( int count = 0; count < objects.Length; count++ )
    24.         {
    25.             pool[ count ] = new List<GameObject>();
    26.             for( int num = 0; num < number[ count ]; num++ )
    27.             {
    28.                 temp = ( GameObject ) Instantiate( objects[ count ] );
    29.                 temp.transform.parent = this.transform;
    30.                 pool[ count ].Add ( temp );
    31.             }
    32.         }
    33.     }
    34.  
    35.     public GameObject activate( int id )
    36.     {
    37.         for( int count = 0; count < pool[ id ].Count; count++ )
    38.         {
    39.             if( !pool[ id ][ count ].activeSelf );
    40.             {
    41.                 pool[ id ][ count ].SetActive ( true );
    42.                 return pool[ id ][ count ];
    43.             }
    44.         }
    45.         pool [ id ].Add ( ( GameObject ) Instantiate ( objects[ id ] ) );
    46.         pool[ id ][ pool[ id ].Count - 1 ].transform.parent = this.transform;
    47.  
    48.         return pool[ id ][ pool[ id ].Count - 1 ];
    49.     }
    50.  
    51.     public GameObject activate( int id, Vector3 position, Quaternion rotation )
    52.     {
    53.         for( int count = 0; count < pool[ id ].Count; count++ )
    54.         {
    55.             if( !pool[ id ][ count ].activeSelf );
    56.             {
    57.                 pool[ id ][ count ].SetActive ( true );
    58.                 pool[ id ][ count ].transform.position = position;
    59.                 pool[ id ][ count ].transform.rotation = rotation;
    60.                 return pool[ id ][ count ];
    61.             }
    62.         }
    63.  
    64.         //I may need to fix this so it doesn't spawn at the last known location
    65.         pool[ id ].Add( ( GameObject ) Instantiate( objects[ id ] ) );
    66.         pool[ id ][ pool[ id ].Count - 1 ].transform.position = position;
    67.         pool[ id ][ pool[ id ].Count - 1 ].transform.rotation = rotation;
    68.         pool[ id ][ pool[ id ].Count - 1 ].transform.parent = this.transform;
    69.         return pool[ id ][ pool[ id ].Count - 1 ];
    70.     }
    71.  
    72.     public void deActivate( GameObject deActivateObject )
    73.     {
    74.         deActivateObject.SetActive ( false );
    75.     }
    76. }
    77.  
    Script B: just to test the spawning
    Code (CSharp):
    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. }
    28.  
    Script C: attached to the prefab object (the sphere in the PoolingObjects folder)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PoolMember : MonoBehaviour
    5. {
    6.  
    7.     public float life;
    8.  
    9.     float timeToDie;
    10.  
    11.     // Use this for initialization
    12.     void OnEnable ()
    13.     {
    14.         timeToDie = life + Time.time;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update ()
    19.     {
    20.         if( Time.time > timeToDie )
    21.         {
    22.             gameObject.SetActive ( false );
    23.         }
    24.     }
    25. }
    26.  

    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.
     
    Last edited: Apr 20, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    try not using "count" in the for loop, the way it's highlighting in the code section looks like it's a reserved word, usually "for (int i = 0, i< ... ; i++)" is sufficient.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Get rid of the ; after your if statements to start.
     
    LeftyRighty likes this.
  4. Yourname

    Yourname

    Joined:
    May 21, 2013
    Posts:
    15
    thank you, I didn't notice the ; after the if statements