Hey guys, I could use some help figuring out what I need to do here. I’m very new to Unity so I’m sure I’m doing something simple and dumb.
I’m recreating classic minesweeper as a learning exercise and get the error:
NullReferenceException: Object reference not set to an instance of an object
BoardManager.LayMineAtRandom () (at Assets/Scripts/BoardManager.cs:33)
Here is my BoardManager script:
using UnityEngine;
using System;
using System.Collections;
using Random = UnityEngine.Random;
public class BoardManager : MonoBehaviour {
public int columns = 10;
public int rows = 13;
public int mines = 10;
public GameObject square;
private Transform boardHolder;
public static Element[,] elements;
void BoardSetup(){
boardHolder = new GameObject ("Board").transform;
elements = new Element[rows,columns];
for (int x = 0; x < columns; x++) {
for (int y = 0; y < rows; y++) {
GameObject instance = Instantiate(square, new Vector3(x,y,0f), Quaternion.identity) as GameObject;
instance.transform.SetParent(boardHolder);
}
}
}
void LayMineAtRandom(){
int randx;
int randy;
for (int i = 0; i < mines; i++) {
randx = Random.Range (0,rows-1);
randy = Random.Range (0,columns-1);
if(elements[randx,randy].isMine){
i--;
}else{
elements[randx,randy].isMine = true;
}
}
}
// Uncover all Mines
public static void uncoverMines() {
foreach (Element e in elements)
if (e.isMine)
e.loadTexture(0);
}
public void SetupScene() {
BoardSetup ();
LayMineAtRandom ();
uncoverMines ();
}
}
My Element script as well:
using UnityEngine;
using System.Collections;
public class Element : MonoBehaviour {
public bool isMine;
// Different Textures
public Sprite[] emptyTextures;
public Sprite mineTexture;
// Use this for initialization
void Start () {
// Register in Array
int x = (int)transform.position.x;
int y = (int)transform.position.y;
BoardManager.elements[x, y] = this;
}
// Load another texture
public void loadTexture(int adjacentCount) {
if (isMine)
GetComponent<SpriteRenderer>().sprite = mineTexture;
else
GetComponent<SpriteRenderer>().sprite = emptyTextures[adjacentCount];
}
}
Any advice is greatly welcomed, thank you.