- Home /
How to set different score values to different prefabs of an array?
Hi and good morning,
I am having an issue that I have been trying to take a stab at for the better part of 3 hours and thought I would come here and see if someone knew what I might be doing wrong. I am creating a match 3 game where I want to be able to identify which type of piece in my array was matched and to have it do something based on the match.
For example, if there were blue pieces, red pieces, and green pieces, I could have the blue pieces do nothing, the red pieces add 20 points, and the green pieces add 50 points and text animation.
This is the script I am using to find matches:
private void AddToListAndMatch(GameObject piece)
{
if (!currentMatches.Contains(piece))
{
currentMatches.Add(piece);
}
piece.GetComponent<Piece>().isMatched = true;
}
public void GetNearbyPieces(GameObject piece1, GameObject piece2, GameObject piece3)
{
AddToListAndMatch(piece1);
AddToListAndMatch(piece2);
AddToListAndMatch(piece3);
}
private IEnumerator FindAllMatchesCo() {
yield return new WaitForSeconds(0.2f);
for (int i = 0; i < board.width; i++) {
for (int j = 0; j < board.height; j++) {
GameObject currentPiece = board.allPieces[i, j];
if (currentPiece != null) {
if (i > 0 && i < board.width - 1) {
GameObject leftPiece = board.allPieces[i - 1, j];
GameObject rightPiece = board.allPieces[i + 1, j];
if (leftPiece != null && rightPiece != null) {
if (leftPiece.tag == currentPiece.tag && rightPiece.tag == currentPiece.tag) {
GetNearbyPieces(leftPiece, currentPiece, rightPiece);
}
}
}
if (j > 0 && j < board.height - 1) {
GameObject upPiece = board.allPieces[i, j + 1];
GameObject downPiece = board.allPieces[i, j - 1];
if (upPiece != null && downPiece != null) {
if (upPiece.tag == currentPiece.tag && downPiece.tag == currentPiece.tag) {
GetNearbyPieces(upPiece, currentPiece, downPiece);
}
}
}
}
}
}
}
If someone were able to help me with even an example script with placeholder names and variables, that would be incredibly appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612215055im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pattern Detection 1 Answer
Colour Matching Game: Search board for matches? 2 Answers
Puzzle Piece Match Mechanic 1 Answer
Checking Tiles For Match Problem Using If Statements 1 Answer
Creating a 2D shape matching game? 1 Answer