In the block matching game that I’m currently working on, I have the ability for the game to destroy multiple blocks at a given time, but what I want after the blocks are clicked and destroyed is I want the game to recognize how many blocks have been destroyed and then add points based off of that number. For example:
- if 2 blocks - 4 points
- if 3 blocks - 9 points
- if 4 blocks - 16 points
- etc
The code below is where I’m destroying groups of blocks. Would I need to add the points here? Or call it later in an OnDestroy function? Help is much appreciated!
public void DestroyBlock(GameObject clickedBlock) {
Vector2 blockPos = GetBlockPos(clickedBlock);
if (blockPos != new Vector2(-1,-1)) {
Color texType = clickedBlock.renderer.material.color;
clickedBlock.renderer.material.color = new Color(1f,1f,1f,0f);
// Destroy blocks moving up
if (blockPos.y > 0) {
if (grid[(int)blockPos.x,(int)blockPos.y-1].renderer.material.color == texType) {
DestroyBlock(grid[(int)blockPos.x, (int) blockPos.y - 1]);
}
}
// Destroy blocks going right
if (blockPos.x > 0) {
if (grid[(int)blockPos.x-1,(int)blockPos.y].renderer.material.color == texType) {
DestroyBlock(grid[(int)blockPos.x-1, (int) blockPos.y]);
}
}
// Destroy blocks going down
if (blockPos.y < grid.GetLength(1)-1) {
if (grid[(int)blockPos.x,(int)blockPos.y+1].renderer.material.color == texType) {
DestroyBlock(grid[(int)blockPos.x, (int) blockPos.y + 1]);
}
}
// Destroy blocks going left
if (blockPos.x < grid.GetLength (1)-1) {
if (grid[(int)blockPos.x+1,(int)blockPos.y].renderer.material.color == texType) {
DestroyBlock(grid[(int)blockPos.x+1, (int) blockPos.y]);
}
}
}
}