Using overlapSphere on a grid to detect neighbor state

I’m trying to create a script that goes through a grid and places a cube on the ones the player has toggled. Then I want the script to detect whether two blocks are next to each other and remove the two walls between them. I already have a block broken into 6 walls and a script that works up until it tries to detect its neighbor. I looked around online and it seemed like people were suggesting using the overlapsphere which I’ve never used before but I figured I’d give it a shot. The script doesn’t return any errors but the overlapshere also doesn’t seem to detect its neighbor. I’m still pretty new at scripting so any help would be appreciated and I would be willing to radically change my approach to the script if someone thought a different method would have better optimization. I’m not sure if you need the whole code but I’ll put it in anyways. Thanks in advance.

#pragma strict

var Hit : RaycastHit;
public var Range : int = 100;
public var RMesh : GameObject;

var placementPlanesRoot : Transform;
var hoverMat : Material;
var placementLayerMask : LayerMask;
private var originalMat : Material;
private var lastHitObj : GameObject;

var BuildGroup : GameObject[];
private var structureIndex : int = 0;

private var Inside :  boolean = true;

var Grid : GameObject[];

var objN : Collider[];
var objS : Collider[];
var objE : Collider[];
var objW : Collider[];

var WallN : boolean;
var WallS : boolean;
var WallE : boolean;
var WallW : boolean;

var Floor : GameObject[];
var Roof : GameObject[];
var WallN_A : GameObject[];
var WallS_A : GameObject[];
var WallE_A : GameObject[];
var WallW_A : GameObject[];



function Start () {


}

function Update () {
	var DirectionOfLine : Vector3 = RMesh.transform.TransformDirection(Vector3.forward);
	
if ( Input.GetKey(KeyCode.Tab)){
	Debug.DrawLine( RMesh.transform.position, DirectionOfLine * Range, Color.blue);
		
		if(Physics.Raycast( RMesh.transform.position, DirectionOfLine * Range, Hit, Range)){
		
				if(lastHitObj){
				lastHitObj.renderer.material = originalMat;
				}
			
				if( Hit.collider.gameObject.tag == "Grid"){
					lastHitObj = Hit.collider.gameObject;
					originalMat = lastHitObj.renderer.material;
					lastHitObj.renderer.material = hoverMat;
					lastHitObj.tag = "ClosedGrid";
					CheckStatus();	
				}
		}
	}
}

function CheckStatus() {
var center: Vector3 = transform.position;

	for(var cnt : int = 0; cnt < 100; cnt ++){
		objN = null;
		objS = null;
		objE = null;
		objW = null;
		WallN = false;
		WallS = false;
		WallE = false;
		WallW = false;
		
		objN = Physics.OverlapSphere(Grid[cnt].transform.position + Vector3(0,0,20), 200, placementLayerMask);
					if (objN.Length > 0){
					if (objN[1].gameObject.tag == "ClosedGrid"){
						WallN = true;
						}
					}
		objS = Physics.OverlapSphere(Grid[cnt].transform.position + Vector3(0,0,-10), 20, placementLayerMask);
				if(objS.Length > 0){
					if (objS[1].gameObject.tag == "ClosedGrid"){
						WallS = true;
						}
					}
		objE = Physics.OverlapSphere(Grid[cnt].transform.position + Vector3(10,0,0), 20, placementLayerMask);
				if(objE.Length > 0){
					if (objE[1].gameObject.tag == "ClosedGrid"){
						WallE = true;
						}
					}
		objW = Physics.OverlapSphere(Grid[cnt].transform.position + Vector3(-10,0,0), 20, placementLayerMask);
				if(objW.Length > 0){
					if (objW[1].gameObject.tag == "ClosedGrid"){
						WallW = true;
						}
						
					else
					Debug.Log("nope");
					}
			if 	(Grid[cnt].gameObject.tag == "ClosedGrid"){
					
				Floor[cnt] = Instantiate(BuildGroup[0],Grid[cnt].transform.position, Quaternion.identity);
				Roof[cnt] = Instantiate(BuildGroup[1],Grid[cnt].transform.position, Quaternion.identity);
			
				if (WallN == false){
				WallN_A[cnt] = Instantiate(BuildGroup[2],Grid[cnt].transform.position, Quaternion.identity);
					}
				else
					WallN_A = null;
				
				if (WallS == false){
					WallS_A[cnt] = Instantiate(BuildGroup[3],Grid[cnt].transform.position, Quaternion.identity);
					}
				else
					WallS_A = null;
				
				if (WallE == false){
					WallE_A[cnt] = Instantiate(BuildGroup[4],Grid[cnt].transform.position, Quaternion.identity);
					}
				else
					WallE_A = null;
				
				if (WallW == false){
					WallW_A[cnt] = Instantiate(BuildGroup[5],Grid[cnt].transform.position, Quaternion.identity);
					}
				else
					WallW_A = null;	
				}
			}
			
}

Ray/Sphere casting is for when you mostly care about the game world. Maybe grid pos 3,7 has a duck off near the top, but what you’re trying to add is small, so will fit.

If you have a strict grid – one item per space, and an item walking between two spaces only “frees up” the old space when it completely enters the new one (what the old WarCraft for PS did) – if you have that, just use an 2D array that “knows” about each spot. Ex (this is C#, but same idea):

Transform[,] Map = new Transform[20,20];

// check if my right neighbor is green. I am at x,y:
if(x+1<20 && Map[x+1,y]!=null && Map[x+1,y].CompareTag("green"))

Idea is that (x,y-1) is always the thing north of you. You don’t have to know it’s 10 meters away in the world, or is on layer 6… .