I am trying to detect if colors are matched (2x2) in Unity2D, but it doesn't fully work. I have 16 tiles with 4 different colors. I am detecting if the colors are matched with a script, which is attached to each collider which collides with 4 tiles around it. In front of each colider is a button, which rotates the 4 tiles around that button (collider) to the right. The scripts of the colliders have a list called childTiles. The list contains the 4 tiles around that collider, to detect if there are 4 tiles of the same color in one list. It looks like this. (GIF) https://gyazo.com/a2a5c8a5051e937338807fb5ecfaa4ec
This script is attached to each collider. There are 9 colliders in total (each one behind a button).
public GameObject GameManager;
public List<GameObject> ChildTiles = new List<GameObject>();
public GameObject[] ParentPositions;
//public GameObject Tiles;
public bool canTurn = true;
private TileController parentPositions;
private float _rotationDegrees;
private GameManager gameManager;
public Color Green;
public Color Red;
public Color Blue;
public Color Yellow;
public bool greenComplete = false;
public bool redComplete = false;
public bool blueComplete = false;
public bool yellowComplete = false;
public GameObject Topleft; //colliders (only these colliders are necessary out of the 9)
public GameObject Topright;
public GameObject Bottomleft;
public GameObject Bottomright;
public GameObject tileGreen1;
public GameObject tileRed1;
public GameObject tileBlue1;
public GameObject tileYellow1;
void Awake()
{
if (gameObject.name == "Top-left" || gameObject.name == "Top-right" || gameObject.name == "Bottom-left" || gameObject.name == "Bottom-right")
{
Green = tileGreen1.GetComponent<SpriteRenderer> ().color;
Red = tileRed1.GetComponent<SpriteRenderer> ().color;
Blue = tileBlue1.GetComponent<SpriteRenderer> ().color;
Yellow = tileYellow1.GetComponent<SpriteRenderer> ().color;
}
GameManager = GameObject.Find ("GameManager");
//Tiles = GameObject.Find ("Tiles");
ParentPositions = GameObject.FindGameObjectsWithTag ("Position");
}
void Update()
{
greenComplete = parentPositions.greenComplete;
redComplete = parentPositions.redComplete;
blueComplete = parentPositions.blueComplete;
yellowComplete = parentPositions.yellowComplete;
}
void Start()
{
gameManager = GameManager.GetComponent<GameManager> ();
_rotationDegrees = 0f;
foreach (GameObject go in ParentPositions)
{
parentPositions = go.GetComponent<TileController> ();
}
}
public void SquareClicked()
{
if (parentPositions.canTurn == true && gameManager.turns > 0f)
{
gameManager.turns -= 1;
gameManager.turnsText.text = "Turns: " + gameManager.turns;
foreach(GameObject go in ParentPositions)
{
parentPositions = go.GetComponent<TileController> ();
parentPositions.canTurn = false;
}
foreach(GameObject tile in ChildTiles)
{
tile.transform.parent = gameObject.transform;
}
StartCoroutine(Rotate()); //this is where you do the rotation of the parent object.
if (gameManager.turns == 0f)
{
gameManager.turnsText.text = "No turns left";
}
}
}
IEnumerator Rotate()
{
LeanTween.rotateZ (gameObject, _rotationDegrees - 90f, 0.5f);
_rotationDegrees -= 90f;
if (_rotationDegrees == -360f)
{
_rotationDegrees = 0f;
}
yield return new WaitForSeconds (0.5f);
parentPositions.canTurn = true;
yield return new WaitForSeconds (0.5f);
if (parentPositions.greenComplete == false)
{
parentPositions.greenComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Green);
}
if (parentPositions.redComplete == false)
{
parentPositions.redComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Red);
}
if (parentPositions.blueComplete == false)
{
parentPositions.blueComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Blue);
}
if (parentPositions.yellowComplete == false)
{
parentPositions.yellowComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Yellow);
}
Debug.Log ("_greenComplete " + parentPositions.greenComplete);
Debug.Log ("_redComplete " + parentPositions.redComplete);
Debug.Log ("_blueComplete " + parentPositions.blueComplete);
Debug.Log ("_yellowComplete " + parentPositions.yellowComplete);
if (parentPositions.greenComplete && parentPositions.redComplete && parentPositions.blueComplete && parentPositions.yellowComplete)
{
Debug.Log ("Level Complete");
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Tile")
{
ChildTiles.Add (col.gameObject);
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.gameObject.tag == "Tile")
{
ChildTiles.Remove (col.gameObject);
}
}
As you can see in the GIF, there is a problem. The problem is that when there are 4 colors matched, it doesn't return True. It only returns True if you click a button with 4 tiles of the same color around it. Only then it returns True. So you match 4 colors, it doesn't return True, you click the button with those 4 colors around it, it returns True. How do I make it so that it immediately returns True, if 4 colors are matched? As you can see I only use the top-left, top-right, bottom-left and bottom-right collider, as these are the only possible colliders to have 4 tiles with the same color in them at the same time.