Storing arrays through extended editors

I am trying to build a pathfinding system for the game I am working on. I would like to have the graph built prior to the play button being pressed by using a custom editor.The basic idea is that the user chooses the graphs length, width and offset and presses a button to generate the graph.

below is a code example of my custom editor:

@CustomEditor(Graph) 

class GraphEditor extends Editor 
{
	function OnInspectorGUI() 
	{
        GUILayout.Label("Graph Editor");
		
		target.length = EditorGUILayout.IntField ("Length", target.length );
		target.width = EditorGUILayout.IntField ("Width", target.width );
		target.offset = EditorGUILayout.IntField ("Offset", target.offset );
		
		if( GUILayout.Button( "Build Graph" ) )
		{
			target.Generate();
		}
		
		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.PrefixLabel ("graphNodes size: ");
		if( target.graphNodes )
		{
			EditorGUILayout.IntField ( target.graphNodes.length );
		}
		EditorGUILayout.EndHorizontal();
		
		if (GUI.changed)
		{
                   EditorUtility.SetDirty (target);
		}
    }
}

below is an example of what happens when Generate() is called:

var length = 10;
var width = 10;
var offset = 0;
var graphNodes : Array;

function Generate()
{
	//Clean up 
	if( graphNodes )
	{
	   graphNodes.Clear();
	}
	
	//Create a list of nodes
	graphNodes = new GraphNode[ length * width ];
	
	//Instantiate GraphNode and fill list
	var prefab : GraphNode = Resources.Load( "GraphNode", GraphNode );
	for( var i = 0; i < graphNodes.length; ++i )
	{
		graphNodes[ i ] = Instantiate( prefab, Vector3.zero, Quaternion.identity );
	}
	
	//Build graph( not needed for the example )
}

Everything works perfectly, the graph builds and shows all its connections and has a length of ( length * width ) until I press the play button then the arrays length becomes 0. It seems that length, width and offset get set to the taget.script but the array gets cleared somehow.

Does the custom editor have problems with dynamic arrays or is there something I could do differently to fix this?
I have looked around a lot and haven’t been able to find a solution. Any help would be greatly appreciated.

Thank you.

Does your monobehaviour code potentially instantiate an array?

I know that this functionality works fine, even with more complex stuff

The second example is my monobehaviour script, when the Generate button is clicked in my custom editor, it calls the generate function of my monobehaviour script( target.Generate(); ) which then creates a new array( graphNodes = new GraphNode[ length * width ]; ). But when the play button is pressed this array is somehow cleared, while the other data members of this script are not.

Can someone please help me with this!

Arrays changed in edit mode aren’t persistent. Dunno why.

I had to use

@HideInInspector
var variable : Type[ ];

to get the functionality I needed.

Is there any other way to get it to work?

Like ArrayList, I don’t think an Array is a persistent type, use GraphNode[ ].

var length = 10;
var width = 10;
var offset = 0;
var graphNodes : GraphNode[];

perhaps make your GraphNode class a [System.Serializable] System.Object.
or a ScriptableObject, stored as an Assetdatabase