# How can I make an infinte terrain with object pooling in Unity?

I am currently making a 3D endless runner game in unity in C#. I now need to make the platform(3D Tiles) infinite but I don't know how. I already did some research but I don't find the right tutorial.

I already asked a similar question and I got the answer that I need to do it with object pooling but I don't find any good tutorial for object pooling. Does someone know how to make an infinite terrain with object pooling in unity?

Thanks

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

public class TerrainScript : MonoBehaviour
{
public GameObject[] tilePrefabs;

public Transform playerTransform;
private float spawnZ = 4500.56f;
private float tileLength = 2.0f;
private int amtOfTilesOnScreen = 2;

void Start()
{
for (int i = 0; i < amtOfTilesOnScreen; i++)
{
SpawnTile(0);
}
}

void Update()
{
if (playerTransform.position.z > (spawnZ - amtOfTilesOnScreen * tileLength))
{
SpawnTile(0);
}
}

private void SpawnTile(int prefabIndx = 0)
{
GameObject go;
go = Instantiate(tilePrefabs[prefabIndx]) as GameObject;
go.transform.SetParent(transform);
go.transform.position = Vector3.forward * spawnZ;
spawnZ += tileLength;
}
}

• You might want to show what you tried with an object pool. Jun 26, 2018 at 17:48
• I tried something but I deleted it because it didn't work. Jun 26, 2018 at 17:50
• Yep, that's understandable. However, putting this up here would be a great starting point for other users to help you with it :) Jun 26, 2018 at 17:53
• I'm about to head home from work, but keep in mind that the concept of Object Pooling is to reuse instances of the GameObject that you've already spent the cpu time to Instantiate(). Store off a few instances in the array, then when you need one, reposition one you arent currently using. Typically this means you need two arrays, one to hold the objects youre already using, and one to hold the objects you arent. Jun 26, 2018 at 21:18
• If it helps, object pooling is like a treadmill, where you recycle the same few assets you instantiate at the start to give an illusion of an infinite runner. As said above, you would need two arrays: one for the assets currently visible to the player, one for the assets that are not. Unity did a tutorial on an endless runner a while back: unity3d.com/learn/tutorials/topics/scripting/… Jun 26, 2018 at 21:48