I currently generate the maptiles using Tags purely, which works great but doesnt satisfy my needs. I want to delete tiles that i dont need(since my player will only be going to the right) but I have a problem with checking when to delete old tiles so that i can generate new ones. My idea would be checking how close the player is to the last tile given and then delete the first, but i cant do that with tags since tags are given in the order that unity finds the GOs with that tag and not from left to right AFAIK. What would be the best way to get the last ground tile? (the one that got generated last).
using UnityEngine;
using System.Collections.Generic;
public class MapGenerator : MonoBehaviour
{
private GameObject[] groundTiles;
/// <summary>
/// Represents the minimum amount of active tiles needed
/// </summary>
public int ActiveTiles;
void Start()
{
}
void Update()
{
groundTiles = GameObject.FindGameObjectsWithTag("ground");
if (groundTiles.Length < ActiveTiles)
SpawnTile();
}
public void SpawnTile()
{
GameObject tile = (GameObject)Instantiate(groundTiles[groundTiles.Length-1]);
Vector3 tmpVec = tile.transform.position;
tmpVec = tile.transform.position;
tmpVec.x += 1;
tile.transform.position = tmpVec;
}
public void DespawnTile(int tileIndex)
{
List<GameObject> tmp = new List<GameObject>(groundTiles);
tmp.RemoveAt(tileIndex);
groundTiles = tmp.ToArray();
}
}