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

Object Pooling Script for Autorunner?

Discussion in 'Scripting' started by SolidSlish, Dec 10, 2019.

  1. SolidSlish

    SolidSlish

    Joined:
    Nov 8, 2017
    Posts:
    2
    I'm trying to optimise my Mobile Auto-runner Game by replacing Instantiating and Deleting with Object Pooling. However, I can't seem to figure out how to fix my script issue, as asset creation is by far my primary skill, and I'm still a beginner when it comes to scripting. I've been told that I need to provide an INT as a key and I have no idea how to do that. My autorunner uses 4 tiles that loop.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class TileManager : MonoBehaviour
    6. {
    7.     public GameObject[] tilePrefabs;
    8.  
    9.     Dictionary<int, List<GameObject>> tilePool = new Dictionary<int, List<GameObject>>();
    10.  
    11.     private Transform playerTransform;
    12.     private float spawnZ = -10f;
    13.     private float tileLength = 27.7016f;
    14.     private float safeZone = 22.5f;
    15.     private int amnTilesOnScreen = 4;
    16.     private int lastPrefabIndex = 0;
    17.  
    18.     private List<GameObject> activeTiles;
    19.  
    20.     // Start is called before the first frame update
    21.     private void Start()
    22.     {
    23.         activeTiles = new List<GameObject>();
    24.         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    25.  
    26.         for (int i = 0; i < amnTilesOnScreen; i++)
    27.         {
    28.             if (i < 2)
    29.                 SpawnTile(0);
    30.             else
    31.                 SpawnTile();
    32.         }
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     private void Update()
    37.     {
    38.         if (playerTransform.position.z - safeZone > (spawnZ - amnTilesOnScreen * tileLength))
    39.         {
    40.             SpawnTile();
    41.             DeleteTile();
    42.         }
    43.     }
    44.  
    45.     private void SpawnTile(int prefabIndex = -1)
    46.     {
    47.         //make sure prefabIndex is set correctly.
    48.  
    49.         if (!tilePool.ContainsKey(prefabIndex)) tilePool.Add(prefabIndex, new List<GameObject>());
    50.         GameObject go = tilePool[prefabIndex].Find(x => !x.activeSelf); // find disabled gameobject to reuse
    51.         if (go == null)
    52.         {
    53.             go = Instantiate(tilePrefabs[prefabIndex]) as GameObject;
    54.             tilePool[prefabIndex].Add(go);
    55.         }
    56.         //set position for go and else
    57.     }
    58.  
    59.     private void DeleteTile()
    60.     {
    61.         activeTiles[0].SetActive(false);//disable for reuse
    62.         activeTiles.RemoveAt(0);
    63.     }
    64.  
    65.     private int RandomPrefabIndex()
    66.     {
    67.         if (tilePrefabs.Length <= 1)
    68.             return 0;
    69.  
    70.         int randomIndex = lastPrefabIndex;
    71.         while (randomIndex == lastPrefabIndex)
    72.         {
    73.             randomIndex = Random.Range(0, tilePrefabs.Length);
    74.         }
    75.  
    76.         lastPrefabIndex = randomIndex;
    77.         return randomIndex;
    78.     }
    79.  
    80. }
    Before you link me to this other thread (https://forum.unity.com/threads/object-pooling.745133/), I already read it and it did not provide me with the help I needed.

    I would seriously appreciate help as I've been stuck on this issue for quite a while.
     
    Last edited: Dec 10, 2019
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,183
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,747
    I can see you know already how to instantiate game object from prefabs. Now replace Destroy with SetActive(false), prepend Intantiate with checking if such object is in pool and take it from pool instead of instantiation. Use prefabs as keys in your pool dictionary for easy search. If your platforms changing their state during the gameplay, then you also need to write function to revert those changes so objects come from pool brand new without signs of usage
     
  4. SolidSlish

    SolidSlish

    Joined:
    Nov 8, 2017
    Posts:
    2
    I followed tutorials to write these scripts so I really don't understand a lot of what you said (sorry). Can you just show me the changes to make? Also, this script doesn't have destroy in it and I already put set active false. I think it has checking too.