Hi, I am in the process of developing a new board game in unity. This is my first game and am quite new to scripting in C#. I am trying to write some code that shall populate a 2D array class of initial game board data but am receiving the following error:
NullReferenceException: Object reference not set to an instance of an object
GameController.Start () (at Assets/Scripts/GameController.cs:100)
Any help would be appreciated. Thanks.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class GameBoardData {
private float xCoord;
private float zCoord;
private int[,] visorPositons = new int[4,3];
private bool player;
private bool[] gridSpace = new bool[4];
public void SetCoords( float x, float z )
{
xCoord = x;
zCoord = z;
}
public float[] GetCoords()
{
float[] xy = { xCoord, zCoord };
return xy;
}
public void SetVisorPositions( int[,] vp )
{
visorPositons = vp;
}
public int[,] GetVisorPositions()
{
int[,] vp = visorPositons;
return vp;
}
public void SetPlayer( bool pl )
{
player = pl;
}
public bool GetPlayer()
{
bool pl = player;
return player;
}
public void SetGridSpace( bool[] gs)
{
gridSpace = gs;
}
public bool[] GetGridSpace()
{
bool[] gs = gridSpace;
return gs;
}
}
public class GameController : MonoBehaviour {
private float[,,] coordData = new float[8, 11, 2]
{ { {0.0f, 2.88675f}, {5.0f, 0.0f}, {10.0f, 2.88675f}, {15.0f, 0.0f}, {20.0f, 2.88675f}, {25.0f, 0.0f},
{30.0f, 2.88675f}, {35.0f, 0.0f}, {40.0f, 2.88675f}, {45.0f, 0.0f}, {50.0f, 2.88675f} },
{ {0.0f, 8.66025f}, {5.0f, 11.547f}, {10.0f, 8.66025f}, {15.0f, 11.547f}, {20.0f, 8.66025f}, {25.0f, 11.547f},
{30.0f, 8.66025f}, {35.0f, 11.547f}, {40.0f, 8.66025f}, {45.0f, 11.547f}, {50.0f, 8.66025f} },
{ {0.0f, 20.20725f}, {5.0f, 17.3205f}, {10.0f, 20.20725f}, {15.0f, 17.3205f}, {20.0f, 20.20725f}, {25.0f, 17.3205f},
{30.0f, 20.20725f}, {35.0f, 17.3205f}, {40.0f, 20.20725f}, {45.0f, 17.3205f}, {50.0f, 20.20725f} },
{ {0.0f, 25.98075f}, {5.0f, 28.8675f}, {10.0f, 25.98075f}, {15.0f, 28.8675f}, {20.0f, 25.98075f}, {25.0f, 28.8675f},
{30.0f, 25.98075f}, {35.0f, 28.8675f}, {40.0f, 25.98075f}, {45.0f, 28.8675f}, {50.0f, 25.98075f} },
{ {0.0f, 37.52775f}, {5.0f, 34.641f}, {10.0f, 37.52775f}, {15.0f, 34.641f}, {20.0f, 37.52775f}, {25.0f, 34.641f},
{30.0f, 37.52775f}, {35.0f, 34.641f}, {40.0f, 37.52775f}, {45.0f, 34.641f}, {50.0f, 37.52775f} },
{ {0.0f, 43.30125f}, {5.0f, 46.188f}, {10.0f, 43.30125f}, {15.0f, 46.188f}, {20.0f, 43.30125f}, {25.0f, 46.188f},
{30.0f, 43.30125f}, {35.0f, 46.188f}, {40.0f, 43.30125f}, {45.0f, 46.188f}, {50.0f, 43.30125f} },
{ {0.0f, 54.84825f}, {5.0f, 51.9615f}, {10.0f, 54.84825f}, {15.0f, 51.9615f}, {20.0f, 54.84825f}, {25.0f, 51.9615f},
{30.0f, 54.84825f}, {35.0f, 51.9615f}, {40.0f, 54.84825f}, {45.0f, 51.9615f}, {50.0f, 54.84825f} },
{ {0.0f, 60.62175f}, {5.0f, 63.5085f}, {10.0f, 60.62175f}, {15.0f, 63.5085f}, {20.0f, 60.62175f}, {25.0f, 63.5085f},
{30.0f, 60.62175f}, {35.0f, 63.5085f}, {40.0f, 60.62175f}, {45.0f, 63.5085f}, {50.0f, 60.62175f} } };
private bool[,,] gridSpaceData = new bool[8, 11, 4]
{ { { true, false, false, true }, { false, false, true, true }, { true, false, true, true }, { false, false, true, true }, { true, false, true, true }, { false, false, true, true },
{ true, false, true, true }, { false, false, true, true }, { true, false, true, true }, { false, false, true, true }, { true, false, true, false } },
{ { false, true, false, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true },
{ false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, false } },
{ { true, false, false, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true },
{ true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, false } },
{ { false, true, false, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true },
{ false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, false } },
{ { true, false, false, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true },
{ true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, false } },
{ { false, true, false, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true },
{ false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, false } },
{ { true, false, false, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true },
{ true, false, true, true }, { false, true, true, true }, { true, false, true, true }, { false, true, true, true }, { true, false, true, false } },
{ { false, true, false, true }, { false, false, true, true }, { false, true, true, true }, { false, false, true, true }, { false, true, true, true }, { false, false, true, true },
{ false, true, true, true }, { false, false, true, true }, { false, true, true, true }, { false, false, true, true }, { false, true, true, false } } };
public GameBoardData[,] GBD = new GameBoardData[8, 11];
// Use this for initialization
void Start () {
for ( int i = 0; i < 8; i++)
for( int j = 0; j < 11; j++ )
{
GBD[i, j] = new GameBoardData();
GBD[i, j].SetCoords(coordData[i, j, 0], coordData[i, j, 1]); // error
bool[] gs = { gridSpaceData[i, j, 0], gridSpaceData[i, j, 1], gridSpaceData[i, j, 2], gridSpaceData[i, j, 3] };
GBD[i, j].SetGridSpace(gs);
}
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.E))
Application.Quit();
}
}