[Help]Collapse-look-a-like!!

Hey guys I have been working on a collapse-look-a-like but I need some guidance please!

The way I have started working is like this:

I have positioned empty gameobjects in a grid like formation. I then did a script called Instantiate as follows:

var ColorCube : GameObject[];
var RandomNumber: int = 0;
function Start() 
{
RandomNumber = Random.Range(0,4);
Instantiate(ColorCube[RandomNumber] , transform.position, transform.rotation);
}

It instantiates either a red,purple,green, or blue Prefab which I did. and it works as intended and the prefabs are tagged respectively.

Each Cube prefab has a script also as follows:

function OnMouseDown () 
{
if(gameObject.tag == "Blue"  )
{
Debug.Log("Hit Blue");
}
}

and when I click on the prefab it works also.

But now I am stuck. I have no idea on how to check the prefabs which are on the left,right,up and down so that if I have 3 or more of the same color they get destroyed. Could you please guide me on the right track. and if this is not the way to do it could you please like pinpoint me on the right track?

Thank you in advance!!

It would be better if you use 2d array to store the game objects. This would make it easier to solve the problems of finding a match. You can loop through each prefab in the array and check the left,right, up and down one very easily.

To check is simply look at each adjacent prefabs in the array, and see if they share the same attribute ( in your case, tag ) then have a counter that keep track of how many match you have found. You might want to save those that share the same attribute in some kind of an array. When you check all the adjacent cells, you then check the counter whether it is 3 or above or not. If it is 3 or above, you transfer all the saved prefabs into another array that keeps track of game objects that are to-be-destroyed.

okay I have changed it into a 2d array, but I have another problem with function OnMouseDown().

I have created a class like this:

var bl:GameObject;
var gr:GameObject;
var re:GameObject;
var cubecollection:GameObject[];
var colorcollection:String[];
var cubestorage:Array; //creates all instantiated cubes
static var ElementID7 : int = 0;



class Cube extends System.Object
{
	var cprefab:GameObject;//since it is a prefab so it has to be of type GameObject
	var id:int;
	var cubecolor:String;
	
	function Cube(cprefab:GameObject,id:int,cubecolor:String)
		{
			this.cprefab=cprefab;
			this.id=id;
			this.cubecolor=cubecolor;
		}
		
}




function Start()
{
	cubestorage = new Array();
	
	createCubes();
	drawCubes();
}



function Update () {

//bl.transform.position.x = 15;

Debug.Log("The seventh cube has: " + cubestorage[6].id); 
Debug.Log("The seventh cube has color of: "+ cubestorage[6].cubecolor);
ElementID7 = cubestorage[6].id;

}

function OnMouseDown()
{
var cube:Object;	

Debug.Log(cube.id);


}


function createCubes()
{
	var cube:Object;
	var counter:int =0;
	var randomnumber:int =0;
	
	for (i=0; i<4; i++)
		{
			for(j=0;j<4;j++)
			{
				randomnumber = Random.Range(0,3);
				
				//Instantiate(cubecollection[Random.Range(0,3)],Vector2(i,j),Quaternion.identity);
				cube = new Cube(cubecollection[randomnumber],counter++,colorcollection[randomnumber]);
				cubestorage.Add(cube);
				Debug.Log(cube.id);
			}
		}
		
}

function drawCubes()
{
	var xcounter:int = 0;
	var ycounter:float = 0;
	var xgap:float=0;
	
	
	//Debug.Log(cubestorage.length);
	for(i=0;i<cubestorage.length;i++)
	{
		
		Instantiate(cubestorage[i].cprefab,Vector2(xgap,ycounter),Quaternion.identity); //(prefab,position,rotation)
		xcounter++;
		xgap +=1.25;
		
		if (!(xcounter%4))
		{
			xcounter=0;
			ycounter+=1.5;
			xgap =0;
		}
		
	}
}


//need array to hold vertical cubes of same color
//need array to hold horizontal cubes of same color

So now each cube has an id. How do I do it so that I get the id whenever I press on a specific cube. Like cube number 6 will have an id of 5? Could anyone please help me with this issue?
Thank you!