- Home /
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[i].GetComponent<Nodo>());
}
for (int i = 0; i < nodos.Count; i++) {
bosque[i].Add(nodos[i]); //It throws an error here. (NullReferenceException: Object reference not set to an instance of an object)
}
}
}
What am I doing wrong?
Answer by Bunny83 · Dec 10, 2012 at 11:46 PM
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 ;)
Answer by demilp · Dec 10, 2012 at 11:57 PM
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 Extending the Editor
Your answer
Follow this Question
Related Questions
A* optimization and path help 0 Answers
A* Algorithm Node Grid Generation 2 Answers
Unity 3.5: What is the definition of PLE algorithm? 1 Answer
Preventing lines from overlapping 1 Answer
Help with A* algorithm 0 Answers