Detect Blocks Touch

Hi, I’m fairly new to Unity and am wondering how I can make it so when three blocks of the same color are touching, they will disappear and turn into points. Right now, I can’t seem to find out how to make the game know when three blocks of the same color are touching. I have some pictures if that can help. 49815-screenie2.png

There is no easy answer to this but I will answer the big lines on how I would achieve this.

First you need a 2d array that will stores ints or even better an enum like this:

public enum Colors{Empty, Red, Brown, Green, Blue]; 
//etc... put it outside the class if you need it elsewhere!

Colors[,] grid;
const int sizeOfGridX = 20; 
const int sizeOfGridY = 30; //for example

void Start()
{
    grid = new Colors[sizeOfGridX,sizeOfGridY];
}

// a method to store colors to the grid could be like this

public void SetColorInGrid(Vector2 position, Colors color)
{
    grid[position.x,position.y]=color;
}

Then to achieve what you want you need to check the colors of the grid everytime you put a shape in by calling CheckForCombinations():

Don’t forget to add this since we are using a List:

using System.Collections.Generic;

Then:

//You will need those both variables inside the class:
int numberOfMatch=0;
List<Vector2> positionsToRemove = new List<Vector2>();    

public void CheckForCombinations()
{
    numberOfMatch=0;
    positionsToRemove.Clear();
    for(int y=0; y<sizeOfGridY; y++)
    {
        for(int x=0; x<sizeOfGridX; x++)
        {
            //grid is being read from bottom to top and left to right
            if(grid[x,y]!=Colors.Empty)
            {
                CheckRecursively(new Vector2(x,y));
                if(numberOfMatch>1) // if 2 or more of the same color are touching
                {
                    //do something with positionsToRemove for example clear it:
                    for(int i=0; i<positionsToRemove.Count; i++)
                    {
                        grid[positionToRemove_.x, positionToRemove*.y)=Colors.Empty;*_

}
}
positionsToRemove.Clear(); // clear the list
numberOfMatch=0;
}
}
}
}

//now the actual test in a recursive form
void CheckRecursively(Vector2 position)
{
//add the position to the list
positionsToRemove.Add(position);
//care to not get out of the limit of your array! Aslo make sure you never use the same position again or you will fall in infinite loop!
if(position.x<sizeOfGridX-1 && grid[x,y]==grid[x+1,y] && !ListContain(x+1,y)) // if right is the same color and not already treated
{
numberOfMatch++;
CheckRecursively(new Vector2(x+1,y));
}
if(position.x>0 && grid[x,y]==grid[x-1,y] && !ListContain(x-1,y)) // same for left
{
numberOfMatch++;
CheckRecursively(new Vector2(x-1,y));
}
if(position.y<sizeOfGridY-1 && grid[x,y]==grid[x,y+1] && !ListContain(x,y+1)) // and top
{
numberOfMatch++;
CheckRecursively(new Vector2(x,y+1));
}
if(position.y>0 && grid[x,y]==grid[x,y-1] && !ListContain(x,y-1)) // and bottom
{
numberOfMatch++;
CheckRecursively(new Vector2(x,y-1));
}
}

//now you only need to do that ListContain fonction

bool ListContain(Vector2 position)
{
for(int i=0;i<positionsToRemove.Count;i++)
{
if(positionsToRemove*==position)*
return true;
}
return false;
}

Those codes were not tested so I hope I didn’t do any mistakes!
Remember to save your scene before running it because of the infinite loops that could(hopefully not) occurs!