Question by
JackAshwell · Jun 25, 2020 at 12:22 PM ·
2darraylist
Sort a list of gameObjects and remove any duplicates one using vector3 positions
So here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MapGenerator : MonoBehaviour {
public int roomLimit;
public float timeDelay = 0.1f;
private Spawner roomMaker; // Links to spawner script
public List<GameObject> middlePoints = new List<GameObject>();
public List<GameObject> rooms;
private int count;
public Player player;
public GameObject portal;
public bool mapGeneration;
static bool portalStop;
public GameObject boss;
public Statistics stat;
void Update() {
if(Input.GetButtonDown("Fire2")) {
f();
}
}
void f() {
if(mapGeneration == false) {
MiddleChecker();
if(count < roomLimit) {
Invoke("Generator", timeDelay); // Delayed start
count += 1;
} else if(count >= roomLimit) {
Invoke("EndChecker", timeDelay); // Delayed start
}
}
portalCheck();
bossCheck();
}
void MiddleChecker() {
GameObject[] middlePointsTemp = GameObject.FindGameObjectsWithTag("MiddlePoint");
middlePoints.AddRange(middlePointsTemp);
for(int i = 0; i < middlePoints.Count-1; i++) {
if(middlePoints[i].transform.position == middlePoints[i+1].transform.position) {
// Duplicate middle point
middlePoints.Remove(middlePoints[i]);
}
}
foreach(GameObject j in middlePoints) {
j.SetActive(false);
j.SetActive(true);
}
}
void Generator() {
foreach(GameObject spawn in GameObject.FindGameObjectsWithTag("SpawnPoint")) {
spawn.GetComponent<Spawner>().Spawn(); // Grabs the spawner part of the object
roomMaker.spawner = spawn;
}
}
void EndChecker() {
MiddleChecker();
foreach(GameObject end in GameObject.FindGameObjectsWithTag("SpawnPoint")) {
end.GetComponent<Spawner>().EndGenerator(); // Grabs the spawner part of the object
roomMaker.spawner = end;
}
if(portalStop == false) {
int endRoom = rooms.Count-1;
Instantiate(portal, rooms[endRoom].transform.position, transform.rotation);
mapGeneration = true;
} else if(portalStop == true) {
int endRoom = rooms.Count-1;
Instantiate(boss, rooms[endRoom].transform.position, transform.rotation);
mapGeneration = true;
}
}
public void MapReset() {
SceneManager.UnloadSceneAsync("Main Game");
SceneManager.LoadScene("Main Game");
player.health = (int) player.startHealth;
player.armour = (int) player.startArmour;
player.money = player.startMoney;
stat.timerReset();
}
public void stopPortal() {
portalStop = true;
}
void bossCheck() {
if(mapGeneration == true & portalStop == true) {
// Last level
GameObject[] tempBoss = GameObject.FindGameObjectsWithTag("Boss");
int arrayCount = tempBoss.Length;
for(int i = 0; i < arrayCount-1; i ++) {
// Delete every boss except one
Destroy(tempBoss[i]);
}
}
}
void portalCheck() {
if(mapGeneration == true & portalStop == false) {
// Level 1-4
GameObject[] tempPortal = GameObject.FindGameObjectsWithTag("Portal");
int arrayCount = tempPortal.Length;
for(int i = 0; i < arrayCount-1; i ++) {
// Delete every boss except one
Destroy(tempPortal[i]);
}
}
}
}
Inside of my MiddleChecker function, I want to be able to sort my middlePoints list and remove any duplicate objects using their vector3 positions. But it doesn't remove every single one of them. Anyone know why?
Thanks!
Comment