void CheckMatchingCells(int cellIndex) { if (cellIndex < 0) { return; } Texture2D selectedFruit = grid.CellGetTexture(cellIndex); if (selectedFruit == null) { return; // empty cell } // Checks all directions until all matches are selected and no pending cells remains List <int> matches = new List <int> (); matches.Add(cellIndex); List <int> pending = new List <int> (); pending.Add(cellIndex); while (pending.Count > 0) { // extract one fruit from the queue int p = pending [0]; pending.RemoveAt(0); // check all 4 directions of that cell for new matches int row = grid.CellGetRow(p); int col = grid.CellGetColumn(p); for (int k = 0; k < directions.Length; k++) { int row1 = row + directions [k] [0]; int col1 = col + directions [k] [1]; if (row1 >= 0 && row1 < grid.rowCount && col1 >= 0 && col1 < grid.columnCount) { int i = grid.CellGetIndex(row1, col1); Texture2D tex = grid.CellGetTexture(i); if (tex == selectedFruit && !matches.Contains(i)) { matches.Add(i); pending.Add(i); } } } } // If there're 3 or more matches remove them if (matches.Count >= 3) { matches.ForEach((int matchingIndex) => { // Remove fruit grid.CellSetTexture(matchingIndex, null); // Produce a nice effect for matching cells grid.CellFlash(matchingIndex, Color.yellow, 0.25f); }); StartCoroutine(MakeFruitsFall()); } }