I have a script saying to only allow an object to instantiate if a boolean is false. But I want to have a proximity around the instantiated object that turns that boolean on and disallow another object to be placed there until the mouse is out of that proximity then turn the boolean back on.
I got all that to work just fine for one object at a time. I place one object and it works for that first one. Then I place a second and it only works for that second object and no longer the first. Place a third it only works for the third object no longer the first nor the second.
I’m pretty much going in circles with this. ha Anyone have an idea?
Here’s the code. Sorry for the wait.
public List<GameObject> allBuildings;
public bool isOverlapping = false;
public float distanceFromBuilding = 200.0f;
void Update ()
{
allBuildings = new List<GameObject>(GameObject.FindGameObjectsWithTag("Building"));
foreach (GameObject structure in allBuildings)
{
float dist = Vector3.Magnitude(Camera.main.WorldToScreenPoint(structure.transform.position) - Input.mousePosition);
if (dist <= distanceFromBuilding)
{
isOverlapping = true;
structure.renderer.material.color = Color.red;
}
else
{
isOverlapping = false;
structure.renderer.material.color = Color.blue;
}
if (Input.GetMouseButtonDown(0) && !isOverlapping)
{
Instantiate(building, transform.position + Vector3.up * 3, Quaternion.identity);
}
}