Having issue with Physics2D.OverlapCircleAll returning incorrect collider

I’m creating a tower defense game with the new 2D stuff and I’ve run into a really strange issue that I’m sure is with my script.

I have multiple tower placement points, represented by BoxCollider2D objects on the placementLayerMask. When I place a tower on one of these points (e.g. PlacementPad_1), then click on a nearby placement pad (e.g. PlacementPad_2), the script sometimes believes I am still tapping on PlacementPad_1. This occurs when the CircleCollider2D (which is on the default layer) attached to the tower for targeting overlaps the BoxCollider2D. Sometimes it corrects itself if an enemy leaves the placed tower’s targeting trigger, but I can’t tell if there’s an actual correlation there.

Here is my script with some comments. I’m certain the problem lies somewhere in here, since I’m still relatively new to C# scripting, but I can’t seem to tease it out. I’ve commented out countless portions of the script and tried several different things, but I always get the same error.

void Update(){
		//When the mouse button is clicked, we cast a 2D ray to see where we've hit
		RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);


		//If the tower purchase menu is not already open, then...
			//we check to see if we've actually hit something. If not, we return. I found this necessary as I was 
			//getting spammed with a null exception when I didn't click on a placement pad. I tried commenting 
			//this out to see if it was the issue. It was not.

			//Creating an array of all colliders found in a circle with a diameter of 0.02f that is created under the
			//mouse click. Any collider not in the placement layer is presumably ignored. I have tried different sized
			//circles, going smaller and smaller, but it  hasn't fixed the issue.
			Collider2D[] col = Physics2D.OverlapCircleAll(hit.transform.position, 0.01f, placementLayerMask);

			//Debug text to see what has been placed in the array. This is where I first notice the error. It seems as
			//though, if there's a CircleCollider2D on top of the placement pad's BoxCollider2D, the array is getting
			//interfered with as in the description above.
			for(int i = 0; i < col.Length; i++){
				Debug.Log("col " + i + " is " + col*.name + " and size is " + col.Length);*
  •   		}*
  •   		//Had to make a bit of an assumption here. With the size of the circle, the first object in the array should*
  •   		//be the object that was clicked on, assuming something was clicked on. This might be overly redundant, but*
  •   		//commenting it out does not fix the issue. From here on out, the script works fine so long as it has the correct*
  •   		//object as the first in the array.*
  •   		if(col[0] != null){*
  •   			Debug.Log(col[0].gameObject.name);*
  •   			lastHitObject = col[0].gameObject;*
  •   			//Debug.Log(lastHitObject.name);*
  •   			focusedPlacement = lastHitObject.GetComponent<PlacementScript>();*
  •   			Vector3 padPos = Camera.main.WorldToScreenPoint(col[0].transform.position);*
  •   			//Debug.Log(padPos);*
  •   			padPos = uiCam.ScreenToWorldPoint(padPos);*
  •   			//Debug.Log(padPos);*
  •   			padPos.z = 0f;*
  •   			//Debug.Log(padPos);*
  •   			if(focusedPlacement.isOpen){*
  •   				purchaseMenu.transform.position = padPos;*
  •   				purchaseMenu.gameObject.SetActive(true);*
  •   				purchaseMenuOpen = true;*
  •   			}*
  •   		}*
  •   	}*

Thanks for taking a look at this!

I figured it out, finally, and I’ll post what I did here in case others run into the same problem. It’s fortunately pretty simple.

If the colliders being raycasted have the same z coordinates, then OverlapCircleAll doesn’t return them. I don’t know why it would return the colliders I previously interacted with, but there you are. I’m also unsure if this is as intended or not.