Minesweeper UI Issue
I'm making a minesweeper game and I am using the code below. from two separate classes.
using System.Collections;
using UnityEngine;
public class Element : MonoBehaviour
{
//bool used to identify whether there is a mine or not hence True/False
public bool mine;
public Sprite[] emptyTextures; //array for sprites
public Sprite mineTexture;
private void Start()
{//Randomly decide whether it is a mine or is not
mine = Random.value < 0.15;
//converts from float to int
int x = (int)transform.position.x;
int y = (int)transform.position.y;
NewGrid.elements[x, y] = this;
}
public void loadTextures (int count) //counts the number of surrounding mines
{
//will check if element is a mine or not
//if yes it loads the texture
if(mine)
GetComponent<SpriteRenderer>().sprite = mineTexture;
//if not it loads a number from the empty textures array
else
GetComponent<SpriteRenderer>().sprite = emptyTextures[count];
}
public bool isCovered () { // checks if sprite is covered
return GetComponent<SpriteRenderer>().sprite.texture.name == "Default";
}
void check (){ //check if player has clicked sprite and check if clicked is mine
//if it is a mine
if (mine) {
//uncover all mines
NewGrid.uncoverMines();
//notify user that they have lost
print("You Lose...LOSER");
} else {
// show adjacent mine number
int x = (int)transform.position.x;
int y = (int)transform.position.y;
loadTextures(Grid.adjacentMines(x, y));
// uncover area without mines
NewGrid.FFuncover(x, y, new bool[NewGrid.w, NewGrid.h]);
// find out if the game was won now
if (NewGrid.isFinished())
print("you win");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewGrid
{
public static int w = 10; //Width
public static int h = 13; //Height
//creates 2D element array that takes in the width and height variables
public static Element[,] elements = new Element[w, h];
//function to uncover mines
public static void uncoverMines()
{
foreach (Element elem in elements)
if (elem.mine)
elem.loadTextures(0);
}
// Find out if a mine is at the coordinates
public static bool mineAt(int x, int y)
{
// If coordinates in range check for mine.
if (x >= 0 && y >= 0 && x < w && y < h)
return elements[x, y].mine;
return false;
}
// Count adjacent mines for an element
public static int adjacentMines(int x, int y)
{
int count = 0;
if (mineAt(x, y + 1)) ++count; // top
if (mineAt(x + 1, y + 1)) ++count; // top-right
if (mineAt(x + 1, y)) ++count; // right
if (mineAt(x + 1, y - 1)) ++count; // bottom-right
if (mineAt(x, y - 1)) ++count; // bottom
if (mineAt(x - 1, y - 1)) ++count; // bottom-left
if (mineAt(x - 1, y)) ++count; // left
if (mineAt(x - 1, y + 1)) ++count; // top-left
return count;
}
// Flood Fill empty elements
public static void FFuncover(int x, int y, bool[,] visited)
{
// Coordinates in Range?
if (x >= 0 && y >= 0 && x < w && y < h)
{
// visited already?
if (visited[x, y])
return;
// uncover element
elements[x, y].loadTextures(adjacentMines(x, y));
// close to a mine? then no more work needed here
if (adjacentMines(x, y) > 0)
return;
// set visited flag
visited[x, y] = true;
// recursion
FFuncover(x - 1, y, visited);
FFuncover(x + 1, y, visited);
FFuncover(x, y - 1, visited);
FFuncover(x, y + 1, visited);
}
}
public static bool isFinished()
{
// Try to find a covered element that is no mine
foreach (Element elem in elements)
if (elem.isCovered() && !elem.mine)
return false;
// There are none => all are mines => game won.
return true;
}
}
My issue is that when I run the build on visual studio code it compiles perfectly but when I attempt to run on game mode I am hit with several errors all saying the same thing. The error is: NullReferenceException: a null value was found where an object instance was required
it keeps pointing to line 19 which has this code NewGrid.elements[x, y] = this;
I didn't initiate the array to a fixed value I just initiated a 2D array so I don't understand why I am getting this error?
Just to be sure, add this assertion to line above the error there:
UnityEngine.Assertions.Assert.IsNotNull(NewGrid.elements,"NewGrid.elements is definately null");
Your answer
Follow this Question
Related Questions
NullReferenceException, Script Error 1 Answer
GetComponent returns null 1 Answer
NullReferenceException 0 Answers
Reference lost in NetworkEntity 0 Answers
NullReferenceException while raycasting? 0 Answers