Hello,
I’m trying create a global class that I can access and modify from outside of the script but I keep getting this error:
NullReferenceException: Object reference not set to an instance of an object
GridSystem.Start () (at Assets/Scripts/Grid/GridSystem.cs:45)
Here’s the code of the class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GridManager : MonoBehaviour
{
private static GridManager _gridManager;
public static GridManager Instance { get { return _gridManager; } }
void Awake()
{
_gridManager = this;
}
public GridSlot[] allGrids = new GridSlot[10100];
public void AddGridSlot(int id, Vector3 pos)
{
allGrids[id] = new GridSlot();
allGrids[id].id = id;
allGrids[id].position = pos;
allGrids[id].leftSlot = id - 1;
allGrids[id].rightSlot = id + 1;
allGrids[id].aboveSlot = id + 100;
allGrids[id].leftSlot = id - 100;
allGrids[id].beingUsed = false;
}
}
And here’s the code where I’m trying to use the class instance outside of the file:
GridManager.Instance.AddGridSlot(i, new Vector3(curX, max.y, curZ));
Thanks for the help