How destroy a object and it's similars

How can I do a logic to destroy all the red cubes together?

If I count the first index of the matrix and [j] together, they don’t group up correctly in either way,just counting each side separately like I did above woked, but doesn’t seem that’s going to work if I did all sides separately.
I did something like this to test, just countig the “j” side:
void CombineSimilar(int i, int j)
{
//the name of the block tha was hit
int number1 = i;
int number2 = j;

for (; j <= 7; j++)
{
if (j+1<=7 && array[number1, number2].typeOfBlock ==
array[i, j + 1].typeOfBlock)
{
array[i, j + 1].obj.transform.parent =
array[number1, number2].obj.transform;
}
else
break;
}
Destroy(array[number1, number2].obj);
}
[8394-untitled-1+copy.jpg|8394]*
*

first you need to make your algorithm to check who are the ones to be destroyed
my guess would be elements connected only in the row, then according to this, check in your matrix the first element that got triggered to be destroyed, then check the elements on the same row that are 1 space to his left or his right, make a recursive method to check chained elements of the same type, add all this elements to name array, when finished the elimination check, start a elimination process, i make a game like that so at least the code is on my mind, hope you understand the main idea. tip: always think the step by step of what you need to do, keep thinking on the steps that are in the mid of what you think till there is no more else to do, thats the way to do fine algorithms

This should do it. I’m int a bit of a rush and obviously cannot test it without recreating more of your game. The first line assumes you have 7 rows. Let me know of any problems:

void CombineSimilar(int i, int j) {
    if (array[i,j].obj != null) {
  	    Destroy (array[i,j].obj);
        array[i,j].obj = null;
        array[i, j].typeOfBlock =  Block.TypeOfBlock.none;
    }
  	if ((i < 7) &&  (array[i, j].typeOfBlock == array[i + 1, j].typeOfBlock))
			CombineSimilar (i+1, j);
	if ((j > 0) && (array[i, j].typeOfBlock == array[i, j-1].typeOfBlock))
			CombineSimilar(i, j-1);
	if ((j < 7) && (array[i, j].typeOfBlock == array[i, j+1].typeOfBlock))
			CombineSimilar(i, j+1);

}

Flood fill algorithm (Pseudo C#):

Cell[,] grid; // Allocate this one of course!
List<Cell> checkedCells; // Allocate too

void Fill(int x, int y)
{
    this.checkedCells.Add(this.grid[x, y]);
    // Do your stuff here
    if (this.CheckBorder(x + 1, y) == true && this.checkedCells.Contains(this.grid[x + 1, y]) == false)
        this.Fill(x + 1, y);
    if (this.CheckBorder(x - 1, y) == true && this.checkedCells.Contains(this.grid[x - 1, y]) == false)
        this.Fill(x - 1, y);
    if (this.CheckBorder(x, y + 1) == true && this.checkedCells.Contains(this.grid[x, y + 1]) == false)
        this.Fill(x, y + 1);
    if (this.CheckBorder(x, y - 1) == true && this.checkedCells.Contains(this.grid[x, y - 1]) == false)
        this.Fill(x, y - 1);
}

bool CheckBorder(int x, int y)
{
    return (minX <= x && x < maxX &&
            minY <= y && y < maxY);
}

Not so pseudo code… XD

Or this version (Less optim, but less code):

Cell[,] grid; // Allocate this one of course!
List<Cell> checkedCells; // Allocate too

void Fill(int x, int y)
{
    if (this.CheckBorder(x, y) == false ||
        this.checkedCells.Contains(this.grid[x, y]) == true)
        return;
    // Do your stuff here
    this.checkedCells.Add(this.grid[x, y]);
    this.Fill(x + 1, y);
    this.Fill(x - 1, y);
    this.Fill(x, y + 1);
    this.Fill(x, y - 1);
}

bool CheckBorder(int x, int y)
{
    return (minX <= x && x < maxX &&
            minY <= y && y < maxY);
}

I hope that will be enough.

Good day.

The @robertbu’s solution throws a stark over flow exeption, if I mix up the @Mikilo’s solution with @robertbu’s 's works xD

void Fill(int i, int j)
	{
        List<Block> checkedCells = new List<Block>(); <-- WHERE SHOULD I PUT THIS?

        if (CheckBorder(i, j) == false ||
            checkedCells.Contains(this.array[i, j]) == true)
            return;

            checkedCells.Add(array[i, j]);

        if ((i < 7) && (array[i, j].typeOfBlock == array[i + 1, j].typeOfBlock))
                Fill(i + 1, j);
		if ((i > 0) &&  (array[i, j].typeOfBlock == array[i - 1, j].typeOfBlock))
            Fill(i - 1, j);
		if((j > 0) && (array[i, j].typeOfBlock == array[i, j - 1].typeOfBlock))
            Fill(i, j - 1);
		if((j < 7) && (array[i, j].typeOfBlock == array[i, j + 1].typeOfBlock))
            Fill(i, j + 1);
	}
	
    bool CheckBorder(int x, int y)
	{
    	return (0 <= x && x <= 7 &&
         	    0 <= y && y <= 7);
	}