I have experience with unity and c# but needed a little help with this one.
How can I despawn level parts after I have already used them during my game. I’m using a list to keep track of the spawned parts but I am having trouble keeping track deleting them once they have gotten out of a certain range from the player.
This is my level generator script so far.
Any help is appreciated. Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGenerator : MonoBehaviour {
// Amount that checks is player is close enough to spawn the next platforms.
private const float PLAYER_DISTANCE_SPAWN_LEVEL_PART = 200f;
// Amount that checks is player is close enough to despawn the previous platforms.
private const float PLAYER_DISTANCE_DELETE_LEVEL_PART = 200f;
[Header("Level Technicalities")]
[SerializeField] private Transform levelPart_Start;
[SerializeField] private List<Transform> levelPartList;
[SerializeField] private MobilePlayerMovement player;
[SerializeField] private List<GameObject> levelListForDespawning;
[Header("Positional Values / Offsets")]
public float xPos = 0;
public float yPos = -1;
public float zPos = 35;
private bool isStart;
private Vector3 lastEndPosition;
private Vector3 firstPlatformPosition;
private bool isSpawningSimilarPart;
int chosenLevelPartIndex;
int lastLevelIndex;
private void Awake()
{
isSpawningSimilarPart = false;
lastEndPosition = levelPart_Start.Find("EndPosition").position;
firstPlatformPosition = levelPart_Start.Find("StartPlatform").position;
int startingSpawnLevelParts = 5;
isStart = true;
for (int i = 0; i < startingSpawnLevelParts; i++) {
SpawnLevelPart();
}
}
private void Update()
{
if (Vector3.Distance(player.transform.position, lastEndPosition) < PLAYER_DISTANCE_SPAWN_LEVEL_PART)
{
SpawnLevelPart();
}
if (Vector3.Distance(player.transform.position, firstPlatformPosition) > PLAYER_DISTANCE_DELETE_LEVEL_PART)
{
DespawnLevelPart(levelListForDespawning[0]);
}
}
private void SpawnLevelPart()
{
if(isStart == true)
{
chosenLevelPartIndex = 0;
isStart = false;
} else {
do {
chosenLevelPartIndex = Random.Range(0, levelPartList.Count);
}
while(chosenLevelPartIndex == lastLevelIndex);
}
lastLevelIndex = chosenLevelPartIndex;
Transform chosenLevelPart = levelPartList[chosenLevelPartIndex];
Vector3 offset = new Vector3(xPos, yPos, zPos);
/*
if (chosenLevelPartIndex == 0) // The Tunnel
{
isSpawningSimilarPart = true;
}
else if (chosenLevelPartIndex == 1) // Block-Box HARD
{
isSpawningSimilarPart = true;
}
else if (chosenLevelPartIndex == 2) // Block-Box Easy
{
isSpawningSimilarPart = true;
}
else if (chosenLevelPartIndex == 3) // MiniMaze Left
{
isSpawningSimilarPart = false;
}
else if (chosenLevelPartIndex == 4) // MiniMaze Right
{
isSpawningSimilarPart = true;
}
else if (chosenLevelPartIndex == 5) // Slim Pathways
{
isSpawningSimilarPart = false;
}*/
Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition + offset);
levelListForDespawning.Add(lastLevelPartTransform.gameObject);
lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
firstPlatformPosition = levelListForDespawning[0].transform.position;
lastLevelIndex = chosenLevelPartIndex;
}
private Transform SpawnLevelPart(Transform levelPart, Vector3 spawnPosition)
{
Transform levelPartTransform = Instantiate(levelPart, spawnPosition, Quaternion.identity);
return levelPartTransform;
}
private void DespawnLevelPart(GameObject levelPartToDespawn)
{
//Destroy(levelPartToDespawn);
Debug.Log(levelPartToDespawn);
levelListForDespawning.Remove(levelPartToDespawn);
}
}