I know there is a million threads regarding this topic but I just cant seem to figure out how to solve my issue. I have two scripts. One creates a 2d array and one is the class for the 2d array. But I am getting the error when I am trying to create the array. It did work fine before when I was using the array to hold a single int, but now ive changed it to hold a struct and now this error. The full error is
NullReferenceException: Object reference not set to an instance of an object
GridSystem`1[TGridObject]…ctor (System.Int32 width, System.Int32 height, System.Int32 cellsize, UnityEngine.Vector3 originPosition) (at Assets/scripts/GridSystem.cs:49)
InitialSetup.Start () (at Assets/scripts/InitialSetup.cs:17)
I dont know why its complaining about code at line 49 as there is no code there. But the main issue seems to be with the InitialSetup script;
The two scripts are
GridSystem
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct GridData
{
public bool isOccupied;
public bool isBuildable;
}
public class GridSystem<TGridObject>
{
int gridWidth;
int gridHeight;
int cellsize;
Vector3 originPosition;
GridData[,] gridArray;
TextMesh[,] debugArrayText;
bool isBuildable;
public GridSystem(int width, int height, int cellsize, Vector3 originPosition)
{
gridWidth = width;
gridHeight = height;
this.cellsize = cellsize;
this.originPosition = originPosition;
debugArrayText = new TextMesh[gridWidth, gridHeight];
gridArray = new GridData[gridWidth, gridHeight];
}
}
and the Initial Setup script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InitialSetup : MonoBehaviour
{
GridSystem<GridData> grid;
float startingPositionX;
float startingPositionY;
float aspect = (float)Screen.width / Screen.height;
private void Start()
{
startingPositionY = Camera.main.orthographicSize;
startingPositionX = startingPositionY * aspect;
grid = new GridSystem<GridData>(30, 15, 4, new Vector3( -58, -50, 0));
}
}
If anyone can help me figure out what I am doing wrong here, it would be greatly appreciated.