NullReferenceException

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();
    }
}

Lots of posts all over the place addressing this.

Null means that something doesn’t exist. You get a NullReferenceException when you attempt to access a member of something that doesn’t exist. You’re doing that on line 100 of your GameController class.

I uncovered the cause of the problem is that unity doesn’t seem to be able to serialize a 2D class array and therefore not recognize it.
Using this array would be convenient for me. Any way around this or is there some better method that I might employ.
Thanks.

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].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);
            }

Unity should be able to serialize a data container. Try this:

[System.Serializable]
public class GameBoardData {
...
}

Yes, I have since tried that as reflected in my updated code snippet up top. But it still fails to recognise the class in voidStart(){…

I was going over your code 10th time now thinking “This SHOULD be working”…

Then it hit me. I also did the same mistake like a dozen times per project and I just never learn.

for ( int i = 0; i < 8; i++)
            for( int j = 0; j < 11; j++ )
            {       
               // This guddam thing:
              GBD[i,j] = new GBD();

                GBD[i, j].SetCoords(coordData[i, j, 0], coordData[i, j, 1]); // no more error

New array allocation does not create individual instances in the array :smile:

1 Like

Thanks for the insight, but still dosen’t work…

Error CS0246 The type or namespace name ‘GBD’ could not be found (are you missing a using directive or an assembly reference?)

Arrgh, what I meant was,

new GameBoardData();

Apologies!

Thanks, but still getting NullReferenceException error…

Where, on which line? Cannot be same one, I think we sorted that. Can we see your code with all the changes again?

My oversight :eyes:. I had some irrelevant test code in there. Took it out and works just fine now.
Thanks a million! :slight_smile:

No problems.

Also, can’t help but note there was some value hardcoding in your Game Controller class up there… maybe you want to consider generating them with an algorithm?

To be honest game development isn’t my line of expertise. I don’t have a significant knowledge of programming or mathematics so the writing of algorithms was going to be quite a challenge for me.
Ideally what I’m looking for is someone that could take over this project for me so I could concentrate on other things.

I found a university lecturer that proposed he and his students could take it over and develop with unreal engine using a node based system for the a.i. I feel however that unity would be a better development environment for this game as could be achieved with C#.

If interested perhaps I could share what I’ve done so for with you and if you like it I could hand the project over to you and we could go 50/50 on the profits.

Thanks for all the help.

I am not really interested (since I have several ongoing projects of my own), but there is a subforum for something like that.

Best of luck!

BTW yeah Unity will probably get you some prototype results and demo state much faster than Unreal Engine, under assumption that you worked with neither before. UE can be kind of tough at times.

OK thats fine. I hope you do well with your projects. Any suggestions for developing good algorithms? Thanks again.

Yeah, keep working on your projects :smile: No other advice than that.

Whenever you get stuck, google it. If there are no results, there has to be another way, different approach.

Same as with anything else, the more you work with it, the better you are at it. Happy coding :slight_smile: