Boolean on/off works for one instantiated object at a time.

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);
    		}
    		}

Ok, what you’d do in that case is separate the instantiation bit from the ‘isOverlapping’ bit. Don’t set

isOverlapping = false;

at any point except at its original declaration, and immediately before you enter the foreach loop. Is there any reason why it is a public member variable? I’d have thought it’d be something which gets determined on a frame-by-frame basis, and as such should stay inside the scope of the Update method.

After looping through all the buildings and setting their renderer colours appropriately, do something like this-

if(!isOverlapping)
{
    // now do the instantiation stuff
}

Basically, take the instantiation stuff out of the bit where you check for locations, and instead allow the locations thing to return a single value, true or false.