Hi i want to delete rows that get filled by one color in a flood fill game. The problem is that when i delete a row it gives me the null object reference error. the error occurs since the node in floodFill method is the first tile on the grid.
here is an image to show you which rows i mean to delete
Can anyone advise me any way to do this. I do not necessarily need code but a way to do it.
here is my floodfill code
public void floodFill (Color replacementColor)
{
Color targetColor = node.GetComponent<SpriteRenderer>().color;
if (node.GetComponent<SpriteRenderer> ().color != targetColor)
return;
if (node.GetComponent<SpriteRenderer> ().color == replacementColor)
return;
Q.Enqueue (node);
while (Q.Count > 0) {
n = Q.Dequeue ();
Debug.Log ("n" + n.transform.position);
if (FindColorOfGameObjectFromPos (new Vector2 (n.transform.position.x, n.transform.position.y)) == targetColor) {
int w = (int)n.transform.position.x;
int e = (int)n.transform.position.x;
int y = (int)n.transform.position.y;
while ((w > 0) && FindColorOfGameObjectFromPos (new Vector2 (w - 1, y)) == targetColor) {
w--;
}
while (((e < 17) && FindColorOfGameObjectFromPos (new Vector2 (e + 1, y)) == targetColor)) {
e++;
}
for (int i = w; i <= e; i++) {
FindGameObjectFromPos (new Vector3 (i, y)).GetComponent<SpriteRenderer>().color = replacementColor;
if ((y > 0)) {
if (FindColorOfGameObjectFromPos (new Vector2 (i, y - 1)) == targetColor) {
Q.Enqueue (FindGameObjectFromPos (new Vector2 (i, y - 1)));
}
}
if ((y < rows - 1)) {
if (FindColorOfGameObjectFromPos (new Vector2 (i, y + 1)) == targetColor) {
Q.Enqueue (FindGameObjectFromPos (new Vector2 (i, y + 1)));
}
}
}
player.position = new Vector2 (e, y);
}
}
}