I’ve setup a Quadtree data structure that I’m using for some terrain data. I am running into a problem regarding the serialising of List<> objects. My structure is something like the following:
[Serializable]
public class TerrainScript : MonoBehaviour
{
public QuadTree m_QuadTree;
}
[Serializable]
public class QuadTree
{
public QuadTreeNode m_root;
}
[Serializable]
public class QuadTreeNode
{
public List<QuadTreeNode> m_nodes = new List<QuadTreeNode>(); (Getting NullReferenceException when accessing these contents)
}
It works fine, up until I close Unity or run the game and try to continue editing the terrain. It is like it has tried to reconstruct my objects but hasn’t been able to. I get a NullReferenceException whenever anything tries to access the list in QuadTreeNode. Am I missing something when it comes to serialization? Everything is public, and is either a List<> or basic data type, and all classes have the [Serializable] tag. Is there anything else I can try?
That seems to have worked. The error has shifted to a different area of code. It seems like everything is fine, but when I go into Play mode and come back, or close Unity and reopen it. It doesn’t properly reinstantiate my objects.
I am getting “NullReferenceException: Object reference not set to an instance of an object” at this line:
[Serializable]
public class RectangleF
{
public float _x;
public float _y;
public float _width;
public float _height;
public float _x2;
public float _y2;
}
[Serializable]
public class QuadTreeNode
{
public List<QuadTreeNode> m_nodes;
public RectangleF m_bounds;
public RectangleF GetBounds() { return m_bounds; }
}
[Serializable]
public class TerrainScript : MonoBehaviour
{
public void CreateGeometry(QuadTreeNode obj)
{
if (obj.GetEnabled())
{
vertices[m_CurrentVertex].x = obj.GetBounds().X; (exception here only after play/continue or unity open/close)
}
}
}
I’ve been looking into Serialization. Is [Serializable] all I need to add to make my own classes serializable for use in the Unity Editor?