0

I have the attached code and it runs fine in Visual Studio but intermittently works in Unity. The error code is:

NullReferenceException: Object reference not set to an instance of an object.

GridSudoku.SetGridNumbers () (at Assets/Scripts/GridSudoku.cs:78)

GridSudoku.Start () (at Assets/Scripts/GridSudoku.cs:24)

I feel like it might be to do with order of operations because it's clearly "BoardData.instance.current_game.unsolved_data" that is null but I can't understand why. That should be set on Start in the BoardData class.

I've checked all my scripts are in the right place in Unity. It is working usually after I remove and reattach the BoardData script but then if I build or just repeat the process it fails with the same error.

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class GridSudoku : MonoBehaviour
{
    public int columns = 0;
    public int rows = 0;
    public float square_offset = 0.0f;
    public GameObject grid_square;
    public Vector2 start_position = new Vector2(0.0f, 0.0f);
    public float square_scale = 1.0f;

    private List<GameObject> grid_squares_ = new List<GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        if (grid_square.GetComponent<GridSquare>() == null)
            Debug.LogError("This Game Object needs GridSquare script");

        SetGridNumbers();
    }

    public void SetGridNumbers()
    {
        for (int index = 0; index < grid_squares_.Count; index++)
        {
            grid_squares_[index].GetComponent<GridSquare>().SetNumber(BoardData.instance.current_game.unsolved_data[Mathf.FloorToInt(index / 9), (int)Mathf.Repeat(index, 9)]); // THIS LINE IS THE ERROR
        }
    }
}

and

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoardData : MonoBehaviour
{
    public static BoardData instance;
    public struct SudokuBoardData
    {
        public int[,] unsolved_data;
        public int[,] solved_data;
        public int[,,] candidates;

        public SudokuBoardData(int[,] unsolved, int[,] solved, int[,,] candidates) : this()
        {

            this.unsolved_data = unsolved;
            this.solved_data = solved;
            this.candidates = candidates;

        }
    }
    public SudokuBoardData current_game;

    void Start()
    {
            createNewSudoku();
    }

    public void createNewSudoku()
    {
            int[,] unsolved = new int[9, 9];
            int[,] solved = new int[9, 9];
            int[,,] candidates = new int[9, 9, 9];

        int givens = 0;
        do
        {
            unsolved[Random.Range(0, 8), Random.Range(0, 8)] = givens+1;
            givens++;
        } while (givens < 9);

        current_game = new SudokuBoardData(unsolved,solved,candidates);
    }
}
Alistair
  • 589
  • 3
  • 11
  • 1
    [Learning how to use a debugger](https://idownvotedbecau.se/nodebugging/) will help you in this, and in many other problems. – Dour High Arch Nov 07 '20 at 19:08
  • Yeah I'm learning the C#/Unity debugging process now but I'm finding it a lot more difficult to see the issue than other languages – Alistair Nov 07 '20 at 20:02

0 Answers0