Search Unity

Null Reference Exception for a Full Match 3 Board

Discussion in 'Scripting' started by CurtisJensen, May 8, 2019.

  1. CurtisJensen

    CurtisJensen

    Joined:
    Jul 31, 2017
    Posts:
    1
    When I make a match in my match 3 game (like Bejeweled or Candy Crush) it recognizes the horizontal match, destroys the pieces, drops down pieces that are above to fill its place, fills in the top row's gaps, checks for a cascade, and says that the matched pieces have not been replaced (via exception) even though I can see them getting replaced. Here's some of my code; thanks in advance for being awesome at coding!

    Code (CSharp):
    1. void FallIn(Vector2 dotPos)//For every dot that's above this dot, move it down.
    2.     {
    3.         Debug.Log(dotPos.x + ", " + dotPos.y);
    4.         for (int row = (int)dotPos.y + 1; row < height; row++)
    5.         {
    6.             allDots[(int)dotPos.x, row].GetComponent<Dot>().row--;//When this is called all of the pieces lerp down due to their update function
    7.         }      
    8. //Replenish the top row
    9.         int dotToUse = Random.Range(0, dots.Length);
    10.         GameObject dot = Instantiate(dots[dotToUse], new Vector2((int)dotPos.x, height - 1), Quaternion.identity);
    11.         allDots[(int)dotPos.x, height - 1] = dot;
    12.     }
    Code (CSharp):
    1. public void FindMatches()
    2.     {
    3.         matchingTime++;
    4.         //Horizontal Scanning
    5.         for (int row = 0; row < height; row++)//Go through each row.
    6.         {
    7.             for (int column = 1; column < width; column++)
    8.             {
    9.                 //Debug.Log("("+matchingTime+") "+column + ", " + row + " is " + allDots[column, row].tag);
    10.                 if (allDots[column - 1, row].tag == allDots[column,row].tag)
    11.                 {
    12.                     comboCounter++;
    13.                     if(comboCounter > 1)
    14.                     {
    15.                         doomedDots.Add(allDots[column - 2, row].GetComponent<Dot>());
    16.                         doomedDots.Add(allDots[column - 1, row].GetComponent<Dot>());
    17.                         doomedDots.Add(allDots[column, row].GetComponent<Dot>());
    18.  
    19.                         foundMatch = true;
    20.                     }
    21.                 }
    22.                 else
    23.                 {
    24.                     comboCounter = 0;
    25.                 }
    26.             }
    27.             comboCounter = 0; //If I don't put this here two wrapping match 2s will make a match 4
    28.         }
    29.  
    30.         if (foundMatch)
    31.         {
    32.             CollectMinerals();
    33.             Dot.machable = true;
    34.             //TODO
    35.             //Check for matches
    36.         }
    37.     }
    Dot Update that makes it move when it's coordinates change:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         targetX = column; //Will change if the column is changed externally
    4.         targetY = row;
    5.         /*If the column has been changed by another script and does not equal the position anymore*/
    6.         if (Mathf.Abs(targetX - transform.position.x) > .1)
    7.         {
    8.             tempPos = new Vector2(targetX, transform.position.y);//Lock sights on new position
    9.             transform.position = Vector2.Lerp(transform.position, tempPos, .4f);//Go to that new position
    10.  
    11.             if (Mathf.Abs(targetX - transform.position.x) < .1 && machable)//If it did it's job, check for matches
    12.             {
    13.                 machable = false;
    14.                 board.allDots[column, row] = this.gameObject;
    15.                 board.FindMatches();
    16.             }
    17.             position = new Vector2(column, row);//This is required for FallIn
    18.         }
    19.  
    20.  
    21.         if (Mathf.Abs(targetY - transform.position.y) > .1)
    22.         {
    23.             tempPos = new Vector2(transform.position.x, targetY);
    24.             transform.position = Vector2.Lerp(transform.position, tempPos, .4f);
    25.  
    26.             if (Mathf.Abs(targetY - transform.position.y) < .1 && machable)
    27.             {
    28.                 machable = false;
    29.                 board.allDots[column, row] = this.gameObject;
    30.                 board.FindMatches();
    31.             }
    32.             position = new Vector2(column, row);//This is required for FallIn
    33.         }
    34.     }
    If you need more code to understand the issue I'd be happy to add it.