Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with object pooling at Y position

Discussion in 'Scripting' started by Kauesic, Sep 14, 2018.

  1. Kauesic

    Kauesic

    Joined:
    Jul 18, 2017
    Posts:
    1
    Hello guys im trying to make an object pool that instantiates enemies at Y position while the player are swimming up but the pool only instantiate the max amount enemy, then the player pass these enemies the pool deactivates all the objs. I was following a pool tutorial, the pooling script works in horizontal but at vertical aftes the first objs won`t work anymore.
    these are the scripts
    obj pool:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyPool : MonoBehaviour {
    6.  
    7.     public GameObject pooledObjects;
    8.     //quantidade guardada
    9.     public int pooledAmount;
    10.     //lista de objetos guardados nela, no caso os inimigos
    11.     List<GameObject> pooledObjs;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.  
    16.         pooledObjs = new List<GameObject>();
    17.  
    18.         for(int i = 0; i < pooledAmount; i++)
    19.         {
    20.             GameObject obj = (GameObject)Instantiate (pooledObjects);
    21.             obj.SetActive (false);
    22.             pooledObjs.Add (obj);
    23.         }
    24.        
    25.     }
    26.    
    27.     public GameObject GetPoolEnemy()
    28.     {
    29.         for (int i = 0; i < pooledObjs.Count; i++)
    30.         {
    31.             if (!pooledObjs[i].activeInHierarchy)
    32.             {
    33.                 return pooledObjs[i];
    34.             }
    35.  
    36.         }
    37.         GameObject obj = (GameObject)Instantiate (pooledObjects);
    38.         obj.SetActive (false);
    39.         pooledObjs.Add (obj);
    40.         return obj;
    41.     }
    42. }
    43.  

    enemy generator:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyGenerator : MonoBehaviour {
    6.  
    7.     public GameObject enemies;
    8.     public Transform genPoint;
    9.     public float distanceBetween;
    10.     public EnemyPool poolEnem;
    11.  
    12.     [SerializeField]private float enemyType;
    13.  
    14.     [SerializeField]private float enemyPosMin;
    15.     [SerializeField]private float enemyPosMax;
    16.  
    17.  
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         enemyType = enemies.GetComponent<CircleCollider2D>().radius;
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update () {
    27.         if(transform.position.y < genPoint.position.y)
    28.         {
    29.  
    30.             distanceBetween = Random.Range (enemyPosMin, enemyPosMax);
    31.  
    32.             transform.position = new Vector3(transform.position.x, transform.position.y + enemyType + distanceBetween);
    33.  
    34.            // Instantiate(enemies, transform.localPosition, transform.localRotation);
    35.             GameObject newEnemy = poolEnem.GetPoolEnemy();
    36.  
    37.             newEnemy.transform.position = transform.position;
    38.             newEnemy.transform.rotation = transform.rotation;
    39.             newEnemy.SetActive(true);
    40.         }
    41.     }
    42. }
    enemy destroyer:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemyDestroyer : MonoBehaviour {


    public GameObject enemyDestroy;


    // Use this for initialization
    void Start () {
    enemyDestroy = GameObject.Find ("EnemyDestroi");
    }

    // Update is called once per frame
    void Update () {

    if (transform.position.y < enemyDestroy.transform.position.y)
    {
    //Destroy (gameObject);
    gameObject.SetActive(false);
    }

    }
    }
    image example
    image_example.png