I’m trying to set up a City Builder, but my buildings are currently able to be built on top of one another.
I have set up scripts called Placeable Building (attached to all Buildings), and Building Placement (attached to camera).
public class PlaceableBuilding : MonoBehaviour {
public List<Collider> colliders = new List<Collider>();
void onTriggerEnter(Collider c) {
if (c.tag == "Building") {
colliders.Add(c);
}
}
void onTriggerExit(Collider c) {
if (c.tag == "Building") {
colliders.Remove(c);
}
}
}
public class BuildingPlacement : MonoBehaviour {
private PlaceableBuilding placeableBuilding;
private Transform currentBuilding;
private bool hasPlaced;
void Update () {
if (currentBuilding != null && !hasPlaced) {
Vector3 m = Input.mousePosition;
m = new Vector3(m.x,m.y,transform.position.y);
Vector3 p = GetComponent<Camera>().ScreenToWorldPoint(m);
currentBuilding.position = new Vector3(p.x,0,p.z);
if (Input.GetMouseButtonDown(0)) {
if (IsLegalPosition()) {
hasPlaced = true;
}
}
}
}
bool IsLegalPosition() {
if (placeableBuilding.colliders.Count > 0) {
return false;
}
return true;
}
public void SetItem(GameObject b) {
hasPlaced = false;
currentBuilding = ((GameObject)Instantiate (b)).transform;
placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding> ();
}
}
Screenshots of inspector to show that Colliders are trigger colliders and script is attached, other screenshot of placement in game, as colliders ignore rules.
Video of problem taking place.
Thank you for any help provided.

