Search Unity

problem with object pooling in infinite runner

Discussion in '2D' started by alisfdn, Jan 25, 2020.

  1. alisfdn

    alisfdn

    Joined:
    Jan 24, 2020
    Posts:
    1
    hello guys!
    i recently created a infinite runner 2d in unity with 2 different environment that each has 4 parent Game object included sky, ground, trees and... so i make a list and add instantiated these parent Game objects for random using. the problem is: when last random number equals to current random number that takes game object from the list are same , parent game object that is player on it and is using, reposition to next position and under feet of player get empty and player falls!


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    using System.Linq;

    public class LevelGeneration : MonoBehaviour
    {
    public GameObject[] world1;
    public GameObject[] world2;
    List<GameObject> world1List;
    List<GameObject> world1ListB;
    List<GameObject> world2List;
    List<GameObject> world2ListB;
    public GameObject player;


    // float[] width1;
    // float[] width2;
    // Transform[] startPos1, startPos2;
    // Transform[] endPos1, endPos2;


    private Vector2 screenBounds;
    public Camera mainCamera;
    public int iterateCount = 10;

    Vector3 nextPos=new Vector3 (-5,0,0);
    float Width;
    Vector3 currentPos;
    Collider2D groundCollider;
    Collider2D playerCollider;
    Transform[] startPos1, endPos2, startPos2, endPos1;
    Queue<GameObject> queueList;

    public Transform rePositionPoint;

    public Transform generationPoint;
    Time timer;

    // Start is called before the first frame update
    void Start()
    {
    world1List = new List<GameObject>();
    world2List = new List<GameObject>();

    world1ListB = new List<GameObject>();
    world2ListB = new List<GameObject>();

    queueList = new Queue<GameObject>();

    currentPos = transform.position;
    //width1 = new float[world1.Length];
    //width2 = new float[world2.Length];
    startPos1 = new Transform[world1.Length];
    endPos1 = new Transform[world1.Length];
    startPos2 = new Transform[world2.Length];
    endPos2 = new Transform[world2.Length];
    playerCollider = player.GetComponent<Collider2D>();

    screenBounds = mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, mainCamera.transform.position.z));

    for (int i = 0; i < world1.Length; i++)
    {
    GameObject c1 = Instantiate(world1[i]) as GameObject;
    Width = c1.GetComponent<BoxCollider2D>().bounds.size.x; //all width same
    groundCollider = c1.GetComponent<Collider2D>();
    startPos1[i] = c1.transform.Find("startPos");
    endPos1[i] = c1.transform.Find("endOfSet");
    //width1[i] = c1.GetComponent<BoxCollider2D>().bounds.size.x;
    c1.SetActive(false);
    world1List.Add(c1);
    }
    for (int i = 0; i < world1.Length; i++)
    {
    GameObject c2 = Instantiate(world1[i]) as GameObject;
    Width = c2.GetComponent<BoxCollider2D>().bounds.size.x; //all width same
    groundCollider = c2.GetComponent<Collider2D>();
    //startPos1[i] = c1.transform.Find("startPos");
    //endPos1[i] = c1.transform.Find("endOfSet");
    //width1[i] = c1.GetComponent<BoxCollider2D>().bounds.size.x;
    c2.SetActive(false);
    world1ListB.Add(c2);
    }

    for (int i = 0; i < world2.Length; i++)
    {
    GameObject c1 = Instantiate(world2[i]) as GameObject;
    Width = c1.GetComponent<BoxCollider2D>().bounds.size.x; //all width same
    groundCollider = c1.GetComponent<Collider2D>();
    startPos2[i] = c1.transform.Find("startPos");
    endPos2[i] = c1.transform.Find("endOfSet");
    //width1[i] = c1.GetComponent<BoxCollider2D>().bounds.size.x;
    c1.SetActive(false);
    world2List.Add(c1);
    }




    }

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



    if(player.transform.position.x + screenBounds.x > currentPos.x + Width/2)
    {
    GameObject c ;
    int rand1 = Random.Range(0, world1List.Count);
    c = GetPooledObject(world1List)
    c.transform.position = nextPos;
    c.SetActive(true);
    world1List
    nextPos.x += Width;
    currentPos.x = c.transform.position.x;


    }



    }

    GameObject GetPooledObject(List<GameObject> list)
    {
    int rand = Random.Range(0, list.Count);
    if(!list[rand].activeInHierarchy)
    {
    return list[rand];
    }
    GameObject c = list[rand];
    return c;

    }



    }