Recognizing duplicate GameObjects next to another.

Working on a 2d game where blocks fall from the top of a grid. I want it to be more difficult to destroy blocks the same color that are grouped together. What would be the best route for the blocks to recognize that they are next to another block of the same color?

With this you can find all colliders around the object:

OR you can use circle shape


I wrote and tested this pretty quickly. It works, but you need to edit it for your own project. I also hope you got the idea how it works.

var radius : float = 1.0;
var objectNear : boolean;

function Update () {
		objectNear = false;
		var hits : RaycastHit2D[];
		hits = Physics2D.CircleCastAll (Vector2(transform.position.x, transform.position.y), radius,;

		for (var i = 0;i < hits.Length;i++) {
			var hit : RaycastHit2D = hits*;*
  •  	var renderer =  hit.collider.GetComponent(SpriteRenderer);*
  •  	if (renderer && renderer.color == {*
  •  		objectNear = true;*
  •  	}*
  •  }		*
  • }*
    This changes objectNear to true when there red object nearby, and back to false when there is not.