- Home /
Match 3 Detection is not working every time
Hello. I am doing a Match 3 Game and its my first "bigger" project with unity. I tried a lot and started it all over again for like 4 times now. My first tries were with the gravity in unity but i had issues! Now i coded everything by myself but i think that my Match-Detection is bugged. this is what my script is doing:
i can swap 2 gems
then i call a sort-methode where i put every Tile to its right index (i am using a 2d array)
i check for matches Tile by Tile
i let all the matched tiles respawn in the top of my screen and falling down
My Problem is that sometimes only half of the matches is marked as "match", sometimes Tiles disappear and i have no idea why!
Here are some parts of my code(i think those are maybe bugged but i can also upload other parts if u need them.)
Sort-Methode:
(This should sort my Array. If Tiles are Moving their Index isnt the same with the position. This Code should bring them back to the right Index)
void SortArray(){
CopyArray(tiles,tilesCopy);
for(int x=0;x<gridWidth;x++){
for(int y =0; y<gridHeight;y++){
tiles[x,y]=tilesCopy[(int) tiles[x,y].transform.position.x,(int) tiles[x,y].transform.position.y];
}
}
}
FallingDown(Pseudo-Gravity)-Methode: (This just makes my Tiles filling the freespace if Matches are "deleted")
void FallingDown(){
for(int x =0;x<gridWidth;x++){
firstEmpty = null;
for(int y = 0;y<gridHeight;y++){
if (emptySpace[x, y] && !firstEmpty.HasValue)
{
firstEmpty = y;
}
else if (firstEmpty.HasValue && !emptySpace[x,y])
{
tiles[x,y].MoveBlockDown(new Vector3(x,(float)firstEmpty,0));
emptySpace[x,y]=true;
emptySpace[x,(int)firstEmpty]=false;
firstEmpty++;
}
}
}
for(int x =0;x<gridWidth;x++){
firstEmpty=null;
for(int y = 0;y<gridHeight;y++){
if(emptySpace[x,y]){
offScreenTile=true;
for(int w=0;w<gridWidth;w++){
for(int h=0;h<gridHeight;h++){
if(!tiles[w,h].onTheBoard() && offScreenTile){
tiles[w,h].MoveBlockDown(new Vector3(x,y,0));
emptySpace[x,y]=false;
offScreenTile=false;
}
}
}
}
}
}
}
My Code is not the cleanest one because im new to coding. You have an idea what is bugged?
Cheers