How to destroy blocks that are matching in color C#

I’m currently working on a game that is similar to Pet Rescue saga and I was having trouble with figuring out how to store the color variable of the block and find colors of blocks that are the same as that color and then “destroy” that group of blocks. Hopefully someone understands what I’m attempting to ask. I’m struggling to describe what I want accurately, but I will post the code and note the lines where I am running into issues. There is prototyped code within the code at the moment, but please ignore it. In the function “public void DestroyBlocks(Vector2 pos)” I’m attempting to store the color of the block and have it find blocks that are on the top, bottom, left, or right of that block that are the same in color and when the user clicks on one of those blocks, it destroys that group. Also note that “Block.tex” is referencing another CS script. I hope this isn’t too complicated/hope I explained it as much as possible so that someone is able to assist me in this. Thank you in advance for your help.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LuckyLandingGame : MonoBehaviour {
	public class Board {
		public GameObject[,] grid = new GameObject[10,10];
		public Vector2 GetBlockPos(GameObject clickedBlock) {
			for (int x = 0; x < 10; x++) {
				for (int y = 0; y < 10; y++) {
					if (clickedBlock.Equals(grid[x,y])){
						Debug.LogError("Found Block at pos: " +x+ ", " +y);	
						return new Vector2(x*1f,y*1f);
		public void DestroyBlocks(Vector2 pos) {
			//temp variable stores color
			Vector2 temp = gameBoard.grid[x,z].GetComponent<Block>().tex;
			//change color to blank color or destroy element
	//var bookNerd
	//var encyclopediaBlock
	//var keyOfKnowledge
	//int confettiPoints
	public Board gameBoard;
	public GameObject block1;
	public Texture blockTexture;
	public List<Color> texBlock = new List<Color>(3);
	public List<Block> blockTypes = new List<Block>();
	int gridWidth = 10;
	int gridHeight = 10;
	//Random rndGrid = new Random();
	// Use this for initialization
	void Start () {
		//randomize colors of blocks (for loop)
		//put player character at the top of stack
		//if user hovers over group of blocks, highlight group
		createWorld ();
	// Update is called once per frame
	void Update () {
		if(Input.GetMouseButtonDown(0)) {
			Ray blockRay = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if(Physics.Raycast(blockRay, out hit)) {
				if (hit.transform.gameObject.GetComponents<Block>().Length>0) {
					Destroy (hit.transform.gameObject);
		//if user clicks on 2 or more blocks - destroy blocks
			//if 2 blocks match +4 points
			//if 3 blocks match +9 points
			//if 4 blocks match +16 points
			//if 5 blocks match +25 points
			//if 6 blocks match +36 points
			//if 7 blocks match +49 points
			//if 8 blocks match +64 points
		//if user clicks on "pause button" pause game
		//if user clicks on "resume button" unpause game
	void powerUps () {
		//bookNerd powerup
			//enables player to eliminate entire HORIZONTAL row of blocks
			//+4 confetti points for every block
		//encylopediaBlock powerup
			//cannot move unless "unlocked"
			//if player has key of knowledge, unlock encyclopediaBlock
		//keyOfKnowledge powerup
			//allow player to "unlock" encyclopediaBlock
	void createWorld () {
		//Create a grid of blocks that chooses a random number and assigns an asset to that number
		for (int x = 0; x<gridWidth; x++) {
			for (int z = 0; z<gridHeight; z++) {
				int chooseBlock = Random.Range(0, 3);
				gameBoard.grid[x,z] = Instantiate(block1, transform.position, transform.rotation) as GameObject;
				gameBoard.grid[x,z].GetComponent<Block>().tex = texBlock[chooseBlock];
				gameBoard.grid[x,z].transform.parent = transform;
				gameBoard.grid[x,z].transform.localPosition = new Vector3 (x-4.5f, 1, z-4.5f);
				gameBoard.grid[x,z].renderer.material.mainTexture = blockTexture;
				gameBoard.grid[x,z].transform.localScale = new Vector3 (.95f, .95f, .95f);

Please try to describe your issue a bit more because right now I am not sure what is exactly the problem.

If you need a way to destroy all blocks of the same color that are neighbours of each other then it shouldn’t be too difficult.

Create a collection for storing temporary list of blocks that will be destroyed. Start from the block that was clicked and iterate through all of it’s neighbours (Up, Right, Down, Left). If colors match, then add the neighbour to the collection. If it doesn’t, then stop. Now do the same for neighbours of each block that matched the original one. Finally destroy all the blocks that are in the collection.

Here’s a really rough example:

// this list will contain all matched blocks
private List<Block> matchingBlocks = new List<Block>();

// this variable stores the color we're matching to
private Color matchingColor;

void CheckBlock(Block block)
	if (matchingColor != null)
		// if the color of the current block matches the matching color
		if(matchingColor == block.Color)
			// let's assume that every block object has a list or an array of all it's neighbours called neighbours
			foreach(Block neighbour in block.neighbours)
				// Recursion
		// if the color we're matching to doesn't exist (it's the original block), then assign the current one to it
		matchingColor = block.Color;

void DestroyMatchingBlocks()
	foreach(Block block in matchingBlocks)
	matchingColor = null;

Now you simple need to call the CheckBlock method on a block clicked by the user and right after that call DestroyMatchingBlocks.

Please send me the full code