Altering Collision Based on Proximity

I am currently creating a randomly generated road in Unity. The road itself works exactly as intended, but now the collision becomes the tricky part.

My road is made of square tiles, and I want the player to walk on the interior. What I have done is I set up for separate Box Collider 2D to the prefab which create the cubes in this road.

What I want to do is have each of the tiles check for if there’s a tile to any given side of, and if there is, then disable the respective Box Collider 2D based on that. If there is another tile to the direct left and to the direct right of one tile, it will disable its Box Colliders that lie on the left and right sides. If one detects a tile above and to the right of itself, it will disable the top Box Collider 2d as well as the right, and so on.

Th two major questions here are:

  1. How can I do a check of if another object is within proximity (in this case basically touching it) within the script?

  2. How can I individually turn off Box Collider components on a prefab, rather than the whole lot of them?

Thanks a bunch!

  1. Look at the docs for Raycast and Raycasthit to draw a ray somewhere to see if it hits a collider, that will show you if there is something in the proximity, you could also check it with sphearCast.

  2. To turn off a component of an object you can collect it in a varible first or just find and turn it off like so.

    player.gameObject.GetComponent().enabled = false;