Making a list of list of nodes for Kruskal

I’m trying to do a Kruskal algorithm (pathfinding), but I’m having a problem when putting the nodes list in the list of list of nodes (each node is in a separate list and as the nodes connect, the lists are combined).

Here is my code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Kruskal : MonoBehaviour {
	
	
	public List<List<Nodo>> bosque;
	public GameObject[] goNodos;
	public List<Nodo> nodos;
	
	
	void Start () {
		goNodos = GameObject.FindGameObjectsWithTag("Nodo");
		
		for (int i = 0; i < goNodos.Length; i++) {
			nodos.Add(goNodos*.GetComponent<Nodo>());*
  •  }*
    
  •  for (int i = 0; i < nodos.Count; i++) {*
    

bosque_.Add(nodos*); //It throws an error here. (NullReferenceException: Object reference not set to an instance of an object)
}*_

* }*

}
What am I doing wrong?

2 Answers

2

Where do you actually create your lists? So where do you actualy do

bosque = new List<List<Nodo>>();

and where do you fill the list with lists?

something like that:

bosque.Add(new List<Nodo>());
bosque.Add(new List<Nodo>());
bosque.Add(new List<Nodo>());

Lists don’t grow on trees, they have to be created :wink:

Great! Solved!
Is there any way to visualize it in the inspector?

That's not really an answer... Did you mean to post a comment? If not and you had another problem which you solved in a different way, post what you did. Visualize what? the List of List thing? Not be default because Unity can't serialize such a construct. The default inspector only displays things which are serializable by Unity. However, you can create a custom inspector for your class. It's not that hard to create one, but also not as easy as the usual Unity stuff ;) See <a href="http://docs.unity3d.com/Documentation/Components/gui-ExtendingEditor.html">Extending the Editor</a>