0

I'm having an issue with my struct. I'm working on an implementation of the Monte Carlo Tree Search algorithm and I created a struct called Node which has specific variables. Now I need to store parent as well as child objects of type node. Since a node can be a parent for other nodes, but also child of a node this is important.

[System.Serializable]
public struct Node
{
    public Transform[,] CurrentField_V;
    public Transform[,] CurrentField_H;
    public Transform[,] CurrentField_Boxes;
    public Node [] nodeChildren;
    public Node [] parent;
    public int result;
    public bool isTerminal;
    public bool alreadyChecked;
    public int y;
    public int x;
    public int visitTimes;

This is where the issue is:

public Node Expand(Node v)
{
    Debug.Log("Start EXPANSION");
    //create a new node
    Node newNode = new Node();
    newNode.parent[0] = v; //---------> NULL REFERENCE EXCEPTION
    newNode.CurrentField_V = v.CurrentField_V;
    newNode.CurrentField_H = v.CurrentField_H;
    v.nodeChildren[counter] = newNode;//---------> NULL REFERENCE EXCEPTION
    newNode.visitTimes++;


}

does anyone have a clue how i can solve this issue?

Dennis Soemers
  • 8,090
  • 2
  • 32
  • 55
Joe Kehr
  • 27
  • 3
  • You need to show your constructor, you create a new node but are you creating a new array for the parents and children? Declaring them only sets the reference, you still need something like : parent = new Node[size]; – Everts Oct 27 '17 at 10:51
  • `newNode.parent = new Node[1]; newNode.parent[0] = v;` or initialize it in the constructor. – FCin Oct 27 '17 at 13:52

0 Answers0