Hi,I am doing a simple randomGeneration,my generation code works fine but it has a tendency to spawn more than 1 tile at a give position so i have made the script below to delete the gameobejects if another one of their kind is at their position,but it doesn’t work it deletes non overlapping objects aswell.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DestroyOnTouch : MonoBehaviour {
public List<GameObject> walls = new List<GameObject>();
public List<GameObject> tiles = new List<GameObject>();
void Start ()
{
Invoke ("Destroy",2f);
}
void Destroy()
{
walls.AddRange(GameObject.FindGameObjectsWithTag("Walls"));
tiles.AddRange(GameObject.FindGameObjectsWithTag("Tile"));
StartCoroutine(DestroyWalls());
StartCoroutine(DestroyTiles());
}
IEnumerator DestroyWalls ()
{
yield return null;
for(int c = 0; c < walls.Count; c++)
{
for(int y = 0; y < walls.Count; y++)
{
if(c > y || y > c)
{
//yield return new WaitForSeconds(0.1f);
if(walls
.transform.position == walls[y].transform.position)
{
Destroy(walls[y],0.1f);
walls.Remove(walls[y]);
}
}
}
}
}
IEnumerator DestroyTiles ()
{
yield return null;
for(int c = 0; c < walls.Count; c++)
{
for(int y = 0; y < tiles.Count; y++)
{
if(c > y || y > c)
{
//yield return new WaitForSeconds(0.1f);
if(tiles[c].transform.position == tiles[y].transform.position)
{
Destroy(tiles[y],0.1f);
walls.Remove(tiles[y]);
}
}
}
}
}
}