Editor Crashes when spawning 1k objects

Hey Guys,

I have a tool that spawns nodes that I use for pathfinding. Each node contains 5-6 floats and a list of its neighbors(max of 8, lists aren’t optimized). Anyways, when i try to relaunch a scene that contains ~1600 nodes, the editor crashed giving me the following error …

System memory in use: 22.2 MB.
Failed to postprocess stacktrace
StackOverflowException

If I make 1000 nodes, the editor is fine …

Any general ideas on how I can decrease the memory put onto the stack? I don’t create any local variables while i loop and create the nodes.

Also, any explanation of how the stack works in depth would be appreciated. I’m using “new” and allocating most of the memory on the heap so I don’t know why the stack is getting so large.

I provided some code/pseudocode that explains the logic of how path node are generated.

Class pathNode
{
	int cost;
	list<pathNode> neighbors
}

//Spawning code

GameObject[] nodeList = new GameObject[num_collumns * num_rows];
for(int x = 0, x < num_collumns; x++)
{
	for(int y = 0; y < num_rows; y++)
	{
		index = x + (y * num_collumns);
		Gameobject newNode = Instantiate(PathNodePrefab, new Vector3(x, 0, y), Quaternion.Identity); 
		newNode.getcomponent<pathNode>().Initialize();
		nodeList[index] = newNode;
	}
}

//Figuring out neighbors
bool bLeft, bTop, bRight, bBottom;

for(int x = 0, x < num_collumns; x++)
{
	for(int y = 0; y < num_rows; y++)
	{
		index = x + (y * num_collumns);
		currentNode = nodeList[index];

		//index into it's left, right, top ,bottom and set booleans to whether it is an existing index
		//using that data, you can figure out LeftTop, RightTop, LeftBottom, RightBottom

		currentNode.neighbors.add( ) // add every adjacent neighbor


	}
}

note: Everything happens when the simulator isn’t running. I use a custom editor script to create these nodes. After 1.6k node objects are created, I save the scene. When i reload the scene, Unity would crash.

Creating that many objects at once is the problem (and it’s often recommended to keep as few real objects as possible). If you convert your nodes into simple classes with positional data (and you could write an editor tool to do the conversion and store the info in a single object), you’ll be able to create “as many as you want” at once.

If they’re Game Objects, there’s just no way to make that many at once. You could try reducing the number spawned per frame (using a coroutine or the like).

A Stackoverflow means you have created some kind of circular reference or infinite recursion somewhere. How does your “nodes” looks like? Is it a MonoBehaviour? How does the neighbor array / list looks like? Is it public or private / protected? How is the array populated?

Also what means “relaunch” in your case? Do you mean open the scene in the editor or loading the level at runtime?