I am following this tutorial:
to make a match 3 game like Candy Crush.
My code is identical. No mistakes. I have reviewed it over 50 times and been struggling for over 6 hours to find a solution myself. Towards the end of this code snippet is the issue. When a match is found, it’s not setting isMatched to true. I have isMatched set to public, which allows me to verify this is to be the problem.
If I manually set isMatched to true while in play mode, then it will change the color of the dot as expected, but it should be setting the color on game start if any matches are found or if you move the dots to find matches. No matter what I do, the isMatched is never set to true unless I manually set it in the inspector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dot : MonoBehaviour
{
public int column;
public int row;
public int targetX;
public int targetY;
public bool isMatched = false;
private Board board;
private GameObject otherDot;
private Vector2 firstTouchPosition;
private Vector2 finalTouchPosition;
private Vector2 tempPosition;
public float swipeAngle = 0;
// Start is called before the first frame update
void Start()
{
board = FindObjectOfType<Board>();
targetX = (int)transform.position.x;
targetY = (int)transform.position.y;
row = targetY;
column = targetX;
}
// Update is called once per frame
void Update()
{
if(isMatched)
{
FindMatches();
SpriteRenderer mySprite = GetComponent<SpriteRenderer>();
mySprite.color = new Color(1f, 1f, 1f, .2f);
}
targetX = column;
targetY = row;
if(Mathf.Abs(targetX - transform.position.x) > .1)
{
// Move Towards the Target
tempPosition = new Vector2(targetX, transform.position.y);
transform.position = Vector2.Lerp(transform.position, tempPosition, .6f);
} else
{
// Directly set the position
tempPosition = new Vector2(targetX, transform.position.y);
transform.position = tempPosition;
board.allDots[column, row] = this.gameObject;
}
if (Mathf.Abs(targetY - transform.position.y) > .1)
{
// Move Towards the Target
tempPosition = new Vector2(transform.position.x, targetY);
transform.position = Vector2.Lerp(transform.position, tempPosition, .4f);
}
else
{
// Directly set the position
tempPosition = new Vector2(transform.position.x, targetY);
transform.position = tempPosition;
board.allDots[column, row] = this.gameObject;
}
}
private void OnMouseDown()
{
firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// Debug.Log(firstTouchPosition);
}
private void OnMouseUp()
{
finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
CalculateAngle();
}
void CalculateAngle()
{
swipeAngle = Mathf.Atan2(finalTouchPosition.y - firstTouchPosition.y, finalTouchPosition.x - firstTouchPosition.x) * 180 / Mathf.PI;
// Debug.Log(swipeAngle);
MovePieces();
}
void MovePieces()
{
if (swipeAngle > -45 && swipeAngle <= 45 && column < board.width)
{
// Right Swipe
otherDot = board.allDots[column + 1, row];
otherDot.GetComponent<Dot>().column -= 1;
column += 1;
}
else if (swipeAngle > 45 && swipeAngle <= 135 && row < board.height)
{
// Up Swipe
otherDot = board.allDots[column, row + 1];
otherDot.GetComponent<Dot>().row -= 1;
row += 1;
}
else if ((swipeAngle > 135 || swipeAngle <= -135) && column > 0)
{
// Left Swipe
otherDot = board.allDots[column - 1, row];
otherDot.GetComponent<Dot>().column += 1;
column -= 1;
}
else if (swipeAngle < -45 && swipeAngle >= -135 && row > 0)
{
// Down Swipe
otherDot = board.allDots[column, row - 1];
otherDot.GetComponent<Dot>().row += 1;
row -= 1;
}
}
void FindMatches()
{
if(column > 0 && column < board.width - 1){
GameObject leftDot1 = board.allDots[column - 1, row];
GameObject rightDot1 = board.allDots[column + 1, row];
if (leftDot1.tag == this.gameObject.tag && rightDot1.tag == this.gameObject.tag)
{
leftDot1.GetComponent<Dot>().isMatched = true;
rightDot1.GetComponent<Dot>().isMatched = true;
isMatched = true;
}
}
if (row > 0 && row < board.height - 1)
{
GameObject upDot1 = board.allDots[column, row + 1];
GameObject downDot1 = board.allDots[column, row - 1];
if (upDot1.tag == this.gameObject.tag && downDot1.tag == this.gameObject.tag)
{
upDot1.GetComponent<Dot>().isMatched = true;
downDot1.GetComponent<Dot>().isMatched = true;
isMatched = true;
}
}
}
}
Any help would be greatly appreciated. Thanks.
I do get a notification that comparison is inefficient and have changed it to use CompareTag(). I reverted back to copy the tutorial since that didn’t work either.
I am using unity version 2022.3.0.