Question by
Blijzijn · May 15, 2020 at 02:08 PM ·
destroy objectdestroygameobject
How to delete tiles when ran over to avoid lag?
Hello everyone I'm making a game where i have to avoid objects while running without an end. I want to make the tiles (who randomly spawn behind eachother to make the pad where the player runs) dissapear when ran over so that I avoid the lagg of all tiles that the game is creating. Can somebody help me with this problem? This is the code where im now stuck with without solution.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileManager : MonoBehaviour
{
public GameObject[] tilePrefabs;
public float zSpawn = 0;
public float tileLength = 19;
public int numberOfTiles = 6;
public Transform playerTransform;
void Start()
{
for(int i = 0; i < numberOfTiles; i++)
{
if (i == 0)
SpawnTile(0);
else
SpawnTile(Random.Range(0, tilePrefabs.Length));
}
}
void Update()
{
if (playerTransform.position.z > zSpawn - (numberOfTiles * tileLength))
{
SpawnTile(Random.Range(0, tilePrefabs.Length));
}
}
public void SpawnTile(int tileIndex)
{
Instantiate(tilePrefabs[tileIndex], transform.forward * zSpawn, transform.rotation);
zSpawn += tileLength;
}
Comment
Your answer
Follow this Question
Related Questions
GameControl always null? 1 Answer
Destroy GameObject after certain amount is reached? 1 Answer
How to destroy object after it moves out of screen 7 Answers
Help me destroy anplayer when out of bounds. 2 Answers
how can i destroy the object when life get 0?,how can i destroy a object when life get 0? 0 Answers