I’m working on a custom pathfinding solution right now but it appears unity is clearing my a specific generic list in a custom class I made.
using System.Collections.Generic;
using UnityEngine;
[System.Serializable, HideInInspector]
public class Node
{
public bool walkable;
public Vector3 position;
public Node parent;
[HideInInspector]
public List<Node> neighbours = new List<Node>();
//this is the distance from the start
public float gCost;
//this is the distance from the destination
public float hCost;
public Node(bool _walkable, Vector3 _position)
{
walkable = _walkable;
position = _position;
}
public float fCost
{
get
{
return gCost + hCost;
}
}
}
This is the code for my class.
Another thing I considered was the list maybe being unreachable because of serialisation depth? Is that even possible?
Anyways does anyone have any idea why this is happening?
I think Unity’s JsonUtility actually does have a depth limit of around 7 or so, which is kinda sad. But I think you’re actually missing something even more important about Unity’s serialization, which is that when you are serializing an object graph, it will duplicate any of your referenced objects if they are referenced more than once in your graph.
For example Let’s say you have a graph that looks like this:
A → B ← C
Where B is a neighbor of both A and C. Unity’s serialization will, by default, create three copies of B:
Then when you deserialize, you will have 3 copies of B instead of jsut one that is referenced by both A and C
To get around this you need to assign each node an ID, and serialize neighbor references as a list of IDs, rather than a list of the neighbor nodes directly.