Having trouble saving a custom class in a GameObject via an Editor script

Hi Guys …

I have created some code, which basically dynamically creates a nodemap or grid for the use of pathfinding via an A* algorythm … It worked all fine and dandy, but now I wanted to create this nodemap pre-runtime via an editor script and save the nodemap in a gameobject or prefab for usage at runtime. This almost works like it should. I am able to create the nodemap, but when I try to store it in a gameobject the information concerning my custom class (Node) is somehow just lost in the process.

The snippets of my code, which concerns itself with the saving of the information can be seen here:

public class NodeMapCreator : MonoBehaviour {
	
	
	[MenuItem ("GameObject/Generate NodeMap")] //Place the Generate Nodemap menu item in the GameObject menu
    static void Init () {
		
		
		Object prefab = EditorUtility.CreateEmptyPrefab("Assets/Nodemap.prefab");
		
		
		List<Node> map = new List<Node>();
		//List<int> ints = new List<int>();
	    int scaleX, scaleZ, sizeX, sizeZ, sizeY;
	    float radius, mSizeX, mSizeZ;
	    GameObject theMap;
	    GameObject aUnit;
		LayerMask unitLayer = 1 << LayerMask.NameToLayer("Unit");
		unitLayer = ~unitLayer;
        theMap = GameObject.FindWithTag("Map");
		aUnit = GameObject.FindWithTag("exampleUnit");

I then go onto actually creating the nodemap as a list of Nodes and then I try to store it in the following code:

		GameObject theNodeMap = new GameObject();
		theNodeMap.AddComponent<NodeMap>();
		
		theNodeMap.GetComponent<NodeMap>().setVariables(map, sizeX, sizeZ, sizeY, radius, theMap, unitLayer);
		theNodeMap.name = "NodeMap";
		
		
		EditorUtility.ReplacePrefab(theNodeMap, prefab);

The funny thing is that the line:
theNodeMap.GetComponent().setVariables(map, sizeX, sizeZ, sizeY, radius, theMap, unitLayer);

is able to store all other variables, but just not my list of nodes (map). It just seems to disappear in my prefab.

Any ideas as to why this is? and what I could do to actually save my list of nodes? (nodemap)

Thanks in advance

To show up in the editor, Node needs to have the [System.Serializable] attribute if isn’t a monobehaviour. Also, the Node class cannot have any references to other Node objects (or references to other classes that have Node objects somewhere down the line).

Hmmm … that is a shame … the nature of the nodemap predicts that nodes have to have references to other nodes in a node, as it has to have neighbours … I have created a temporary solution where Node inherits from monobehaviour and the nodes are added as components to a list of GameObjects instead …
This succesfully stores the nodemap, but it is rather clunky and not nice at all …

Welcome to the forum, Blackbottle!

There is a serialisable graph class in this thread that you might find useful.