No matches on Start Help

Hello,

Im currently looking for some help with my match 3 code… I would like it so when my pieces spawn they don’t spawn automatic matches.

public enum PieceType
    {
        EMPTY,  // Empty shell used when pieces game starts
        NORMAL,  // the basic piece
        BREAKABLE,  // An obstacle piece that can be destroyed
        UNBREAKABLE, // Pieces that cannot be broken
        ROW_CLEAR,     // Clears Row
        COLUMN_CLEAR,  // Clears Column
        COLORMATCH,       // Clears all of a color
        COUNT,
    };

    [System.Serializable]
    public struct PiecePrefab
    {
        public PieceType type;
        public GameObject prefab;
    };

    [System.Serializable]
    public struct PiecePosition
    {
        public PieceType type;
        public int x;
        public int y;
    };

    // Grid Size
    public int xDim; // The horizontal size of the grid
    public int yDim; // The vertical size of the grid

    // Time to fill / refill the grid
    public float fillTime; // Higher Fill time makes board fill slower

    //Used to call Level Class
    public Level level;   // Reference to Level

    // Array of piece types that can be defined in the inspector
    public PiecePrefab[] piecePrefabs;
    // GameObject to set a background for our pieces
    public GameObject backgroundPrefab;

    // Used to set our initial pieces in the inspector, for each level
    public PiecePosition[] initialPieces;

    // Dictionary for attaching a piece to its prefab
    private Dictionary<PieceType, GameObject> piecePrefabDict;

    // 2D array to contain all our pieces
    private GamePiece[,] pieces;

    // Used to move pieces in diag (for filling a grid with obstacles)
    private bool inverse = false;

    // Used to know wich pieces will be swapped
    private GamePiece pressedPiece;
    private GamePiece enteredPiece;

    // Set the game over state
    private bool gameOver = false;

    private bool isFilling = false;

    public bool IsFilling
    {
        get { return isFilling; }
    }


    // Use this for initialization
    void Awake () {

        piecePrefabDict = new Dictionary<PieceType, GameObject>();

        // First we need to check if we have keys in our dictionary. If not, add them
        for ( int i = 0; i < piecePrefabs.Length; i++)
        {
            if ( !piecePrefabDict.ContainsKey (piecePrefabs .type))
            {
                piecePrefabDict.Add(piecePrefabs.type, piecePrefabs.prefab);
            }
      
        }

        //Building the Grid the blank piece background
        for (int x = 0; x < xDim; x++)
        {
            for (int y = 0; y < yDim; y++)
            {
                GameObject background = (GameObject)Instantiate(backgroundPrefab, GetWorldPosition(x, y), Quaternion.identity);
                background.transform.parent = transform;
            }
        }

        // Now we need to add our initial and empty pieces on the grid
        pieces = new GamePiece [xDim, yDim];

        // Filling the Board
        for ( int i = 0; i < initialPieces.Length; i++)
        {
            if ( initialPieces .x >= 0 && initialPieces .x < xDim
                && initialPieces .y >= 0 && initialPieces .y < yDim)
            {
                SpawnNewPiece(initialPieces.x, initialPieces.y, initialPieces.type);
            }
        }

        for ( int x = 0; x< xDim; x++)
        {
            for ( int y = 0; y < yDim; y++)
            {
                if (pieces[x, y] == null)
                {
                    SpawnNewPiece(x, y, PieceType.EMPTY);
                }
          
            }
        }

        // Now the grid is filled with empty pieces, we need to replace them by our non-empty pieces with animation
        StartCoroutine(Fill());

    }
 
    // Update is called once per frame
    void Update () {
 
    }

    //
    // Fill the grid
    //
    public IEnumerator Fill()
    {
        bool needsRefill = true;
        isFilling = true;

        while (needsRefill)
        {
            yield return new WaitForSeconds(fillTime);

            while (FillStep())
            {
                inverse = !inverse;
                yield return new WaitForSeconds(fillTime);
            }

            needsRefill = ClearAllValidMatches();
        }
  
        //   Old code    //isFilling = false;

        //-------------------------------------------------
        CheckAllPossibleMatches();
        //-------------------------------------------------
    }

    // Move a piece to fill the grid
    public bool FillStep()
    {
        bool movedPiece = false;

        for (int y = yDim - 2; y >= 0; y--)
        {
            for (int loopX = 0; loopX < xDim; loopX++)
            {
                int x = loopX;

                if (inverse)
                {
                    x = xDim - 1 - loopX;
                }

                GamePiece piece = pieces[x, y];

                if (piece.IsMovable())
                {
                    GamePiece pieceBelow = pieces[x, y + 1];

                    if (pieceBelow.Type == PieceType.EMPTY)
                    {
                        Destroy(pieceBelow.gameObject);
                        piece.MovableComponent.Move(x, y + 1, fillTime);
                        pieces[x, y + 1] = piece;
                        SpawnNewPiece(x, y, PieceType.EMPTY);
                        movedPiece = true;
                    }
                    else
                    {
                        for (int diag = -1; diag <= 1; diag++)
                        {
                            if (diag != 0)
                            {
                                int diagX = x + diag;

                                if (inverse)
                                {
                                    diagX = x - diag;
                                }

                                if (diagX >= 0 && diagX < xDim)
                                {
                                    GamePiece diagonalPiece = pieces[diagX, y + 1];

                                    if (diagonalPiece.Type == PieceType.EMPTY)
                                    {
                                        bool hasPieceAbove = true;

                                        for (int aboveY = y; aboveY >= 0; aboveY--)
                                        {
                                            GamePiece pieceAbove = pieces[diagX, aboveY];

                                            if (pieceAbove.IsMovable())
                                            {
                                                break;
                                            }
                                            else if (!pieceAbove.IsMovable() && pieceAbove.Type != PieceType.EMPTY)
                                            {
                                                hasPieceAbove = false;
                                                break;
                                            }
                                        }

                                        if (!hasPieceAbove)
                                        {
                                            Destroy(diagonalPiece.gameObject);
                                            piece.MovableComponent.Move(diagX, y + 1, fillTime);
                                            pieces[diagX, y + 1] = piece;
                                            SpawnNewPiece(x, y, PieceType.EMPTY);
                                            movedPiece = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
            }

        }

        // Filling Top row
        for (int x = 0; x < xDim; x++)
        {
            GamePiece pieceBelow = pieces[x, 0];

            if (pieceBelow.Type == PieceType.EMPTY)
            {
                Destroy(pieceBelow.gameObject);

                // This is where we fill the board
                GameObject newPiece = (GameObject)Instantiate(piecePrefabDict[PieceType.NORMAL], GetWorldPosition(x, -1), Quaternion.identity);
                newPiece.transform.parent = transform;

                pieces[x, 0] = newPiece.GetComponent<GamePiece>();
                pieces[x, 0].Init(x, -1, this, PieceType.NORMAL);
                pieces[x, 0].MovableComponent.Move(x, 0, fillTime);

                // This is where we set color that spawns
                pieces[x, 0].ColorComponent.SetColor((ColorPiece.ColorType)Random.Range(0, pieces[x, 0].ColorComponent.NumColors));


                movedPiece = true;
            }
        }

        return movedPiece;
    }

    // Used to get the offset of our pieces
    public Vector2 GetWorldPosition(int x, int y)
    {
        return new Vector2(transform.position.x - xDim / 2.0f + x, transform.position.y + yDim / 2.0f - y);
    }

    //Spawn new Piece
    public GamePiece SpawnNewPiece (int x, int y, PieceType type)
    {
        GameObject newPiece = (GameObject)Instantiate(piecePrefabDict[type], GetWorldPosition (x, y), Quaternion.identity);
        newPiece.transform.parent = transform;

        pieces [x, y] = newPiece.GetComponent<GamePiece>();
        pieces [x, y].Init(x, y, this, type);

        return pieces[x, y];
    }

    // Used to know if piece1 is adjacent to piece2
    public bool IsAdjacent ( GamePiece piece1, GamePiece piece2)
    {
        return (piece1.X == piece2.X && (int)Mathf.Abs(piece1.Y - piece2.Y) == 1)
            || (piece1.Y == piece2.Y && (int)Mathf.Abs(piece1.X - piece2.X) == 1);
    }


    

    // Used to know if pieces can match
    public List <GamePiece> GetMatch ( GamePiece piece, int newX, int newY)
    {
        if (piece.IsColored())
        {
            ColorPiece.ColorType color = piece.ColorComponent.Color;
            List <GamePiece> horizontalPieces = new List<GamePiece>();
            List <GamePiece> verticalPieces = new List<GamePiece>();
            List <GamePiece> matchingPieces = new List<GamePiece>();

            // First check Horizontal
            horizontalPieces.Add(piece);

            for ( int dir = 0; dir<= 1; dir ++)
            {
                for ( int xOffset = 1; xOffset < xDim; xOffset++)
                {
                    int x;

                    if ( dir == 0)
                    {
                        // Left
                        x = newX - xOffset;
                    }
                    else
                    {
                        // Right
                        x = newX + xOffset;
                    }
                    if ( x < 0 || x >= xDim)
                    {
                        break;
                    }

                    if ( pieces [ x, newY].IsColored() && pieces[x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if ( horizontalPieces.Count >= 3)
            {
                for ( int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces);
                }
            }

            // Traverse vertically if we found a match ( for L and T shape mathces)
            if ( horizontalPieces.Count >= 3)
            {
                for ( int i = 0; i < horizontalPieces.Count; i++)
                {
                    for ( int dir = 0; dir <= 1; dir++)
                    {
                        for( int yOffset = 1; yOffset< yDim; yOffset ++)
                        {
                            int y;

                            if ( dir == 0)
                            {
                                // Up
                                y = newY - yOffset;
                            }
                            else
                            {
                                // Down
                                y = newY + yOffset;
                            }

                            if ( y < 0 || y >= yDim)
                            {
                                break;
                            }

                            if (pieces[horizontalPieces.X, y].IsColored() && pieces[horizontalPieces.X, y].ColorComponent.Color == color)
                            {
                                verticalPieces.Add(pieces[horizontalPieces.X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if ( verticalPieces.Count < 2)
                    {
                        verticalPieces.Clear();
                    }
                    else
                    {
                        for ( int j = 0; j < verticalPieces.Count; j++)
                        {
                            matchingPieces.Add(verticalPieces[j]);
                        }

                        break;
                    }
                }
            }

            if ( matchingPieces.Count >= 3)
            {
                return matchingPieces;
            }

            // Then check Vertical
            horizontalPieces.Clear();
            verticalPieces.Clear();

            verticalPieces.Add(piece);

            for (int dir = 0; dir <= 1; dir++)
            {
                for (int yOffset = 1; yOffset < xDim; yOffset++)
                {
                    int y;

                    if (dir == 0)
                    {
                        // Left
                        y = newY - yOffset;
                    }
                    else
                    {
                        // Right
                        y = newY + yOffset;
                    }
                    if (y < 0 || y >= yDim)
                    {
                        break;
                    }

                    if (pieces[newX, y].IsColored() && pieces[newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces);
                }
            }

            // Traverse horizontaly if we found a match ( for L and T shape mathces)
            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    for (int dir = 0; dir <= 1; dir++)
                    {
                        for (int xOffset = 1; xOffset < xDim; xOffset++)
                        {
                            int x;

                            if (dir == 0)
                            {
                                // Left
                                x = newX - xOffset;
                            }
                            else
                            {
                                // Right
                                x = newX + xOffset;
                            }

                            if (x < 0 || x >= xDim)
                            {
                                break;
                            }

                            if (pieces[x, verticalPieces.Y].IsColored() && pieces[x, verticalPieces.Y].ColorComponent.Color == color)
                            {
                                horizontalPieces.Add(pieces[x, verticalPieces.Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (horizontalPieces.Count < 2)
                    {
                        horizontalPieces.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < horizontalPieces.Count; j++)
                        {
                            matchingPieces.Add(horizontalPieces[j]);
                        }

                        break;
                    }
                }
            }

            if (matchingPieces.Count >= 3)
            {
                return matchingPieces;
            }
        }

        return null;
    }

    // Clears all Matches
    public bool ClearAllValidMatches()
    {
        bool needsRefill = false;

        for ( int y = 0; y < yDim; y++)
        {
            for ( int x = 0; x < xDim; x++)
            {
                if ( pieces [x, y].IsClearable())
                {
                    List<GamePiece> match = GetMatch(pieces[x, y], x, y);

                    if ( match != null)
                    {

                        //Special Piece Spawn
                        PieceType specialPieceType = PieceType.COUNT;
                        GamePiece randomPiece = match[Random.Range(0, match.Count)];

                        int specialPieceX = randomPiece.X;
                        int specialPieceY = randomPiece.Y;

                        if ( match.Count == 4)
                        {
                            if (pressedPiece == null || enteredPiece == null)
                            {
                                specialPieceType = (PieceType)Random.Range((int)PieceType.ROW_CLEAR, (int)PieceType.COLUMN_CLEAR);
                            }
                            else if (pressedPiece.Y == enteredPiece.Y)
                            {
                                specialPieceType = PieceType.ROW_CLEAR;
                            }
                            else
                            {
                                specialPieceType = PieceType.COLUMN_CLEAR;
                            }
                        }

                        else if ( match.Count >= 5)
                        {
                            specialPieceType = PieceType.COLORMATCH;
                        }

                        for ( int i = 0; i < match.Count; i++)
                        {
                            if ( ClearPiece ( match .X, match .Y))
                            {
                                needsRefill = true;

                                if ( match[i] == pressedPiece || match [i] == enteredPiece)
                                {
                                    specialPieceX = match[i].X;
                                    specialPieceY = match[i].Y;
                                }
                            }
                        }

                        //Spawn Special Piece
                        if (specialPieceType != PieceType.COUNT)
                        {
                            Destroy(pieces[specialPieceX, specialPieceY]);
                            GamePiece newPiece = SpawnNewPiece(specialPieceX, specialPieceY, specialPieceType);

                            if (( specialPieceType == PieceType.ROW_CLEAR || specialPieceType == PieceType.COLUMN_CLEAR)
                                && newPiece.IsColored() && match[0].IsColored())
                            {
                                newPiece.ColorComponent.SetColor(match[0].ColorComponent.Color);
                            }
                            else if ( specialPieceType == PieceType.COLORMATCH && newPiece.IsColored())
                            {
                                newPiece.ColorComponent.SetColor(ColorPiece.ColorType.ANY);
                            }
                        }
                    }
                }
            }
        }

        return needsRefill;
    }
 
    }

}[/I][/I][/I][/I]

That is like a million lines of code, and there’s no formatting. Please just post the part that’s not working as expected, and use code tags.

thank you for your reply.
I apologize for the mass amount of code and lack of formatting, it was late when i posted this.

I have delete much of the unnecessary code and formatted the rest…

My issue is not code not working properly its I do not know how to make it so when spawning tiles at game start they don’t drop down in sets of 3 or more… I am having issues setting my score goal because sometimes you load level and so many matches drop that you start with a high score and a few special pieces and sometime you get nothing.