destroy all same color combination of bricks

Hi Everyone

I am making brick puzzle game.i am facing a problem please help me

11538-sa.png

you can see what type of project I am doing.Here is my question is whenever my ball collide or hit any red colored brick all the red colored brick which collide each other will destroy.i can’t find any solution to do that.how i can destroy all the red colored brick which collide each other.Please suggest me the right ans. how i can destroy same colored combination of bricks.

and sorry for my English

thanks in advance

You could put something like this on all your boxes. Note it assumes that the boxes are tagged “Red” and “Green” for red and green boxes:

#pragma strict

function OnCollisionEnter(col : Collision) {
	if (col.collider.name != "Ball")
		return;
	
	var tag = gameObject.tag;
	
	var hit : RaycastHit;
    // Check the boxes above
	while(Physics.Raycast(transform.position, Vector3.up, hit)) {
		if (hit.collider.tag != tag)
			break;
		hit.collider.enabled = false;
		Destroy(hit.collider.gameObject);
	}
	// Check the boxes below
	while(Physics.Raycast(transform.position, Vector3.down, hit)) {
		if (hit.collider.tag != tag)
			break;
		hit.collider.enabled = false;
		Destroy(hit.collider.gameObject);
	}
	Destroy(gameObject);
}

doing anything with raycast here is overkill, this should be handled in the datamodel. Store references to your boxes in a 2D array for example (as you have rows of boxes). Or create a linked list implementation in which each box has a reference to the top and bottom box.