Question by
Tom_Unsworth · Dec 04, 2017 at 01:42 PM ·
nullreferenceexceptionontriggerenterdictionarydijkstra
Dijkstra shortest path algorithm but i am getting an error; Null reference exception: object reference not set to an instance of an object.
hello i am trying to programme Dijkstra shortest path algorithm but i am getting an error; Null reference exception: object reference not set to an instance of an object. i think it is because my variables palyerpos and enemypos are null when the code for the shortest path runs although i am not sure why they are equal to null. any help is greatly appreciated! i have been using other peoples questions to figure this out but have had no luck.
public class Node : MonoBehaviour
{ public GameObject[] neibors; public Vector2[] directions;
public static int[] distances = new int[4];
static float distance;
int i = 0;
public static GameObject PlayerPos = null, EnemyPos = null;
// Use this for initialization
void Start()
{
var box = gameObject.AddComponent<BoxCollider2D>();
box.isTrigger = enabled;
foreach (GameObject item in neibors)
{
//find distance.
distance = (gameObject.transform.localPosition.x - item.transform.localPosition.x) + (gameObject.transform.localPosition.y - item.transform.localPosition.y);
distance = Mathf.Abs(distance);
int distanc = (int)distance;
distances[i] = distanc;
i++;
}
}
private void Update()
{
}
public void OnTriggerEnter2D(Collider2D other)
{
if (gameObject.tag == "pallet" && other.name == "pacman_1")
{
EnemyPos = gameObject;
}
if (gameObject.tag == "pallet" && other.name == "player")
{
PlayerPos = gameObject;
}
}
public void Main()
{
Graph g = new Graph();
if (neibors.Length == 2)
{
g.add_vertex(gameObject, new Dictionary<GameObject, int>() { { neibors[0], distances[0] }, { neibors[1], distances[1] } });
Debug.Log(gameObject);
}
if (neibors.Length == 3)
{
g.add_vertex(gameObject, new Dictionary<GameObject, int>() { { neibors[0], distances[0] }, { neibors[1], distances[1] }, { neibors[2], distances[2] } });
Debug.Log(gameObject);
}
if (neibors.Length == 4)
{
g.add_vertex(gameObject, new Dictionary<GameObject, int>() { { neibors[0], distances[0] }, { neibors[1], distances[1] }, { neibors[2], distances[2] }, { neibors[3], distances[3] } });
Debug.Log(gameObject);
}
g.shortest_path(PlayerPos, EnemyPos).ForEach(x => Debug.Log(x));
}
} public class Graph { Dictionary > vertices = new Dictionary >();
public void add_vertex(GameObject gameObject, Dictionary<GameObject, int> edges)
{
vertices[gameObject] = edges;
}
public List<GameObject> shortest_path(GameObject PlayerPos, GameObject EnemyPos)
{
var previous = new Dictionary<GameObject, GameObject>();
var dist = new Dictionary<GameObject, int>();
var nodes = new List<GameObject>();
List<GameObject> path = null;
foreach (var vertex in vertices)
{
if (vertex.Key == PlayerPos)
{
dist[vertex.Key] = 0;
}
else
{
dist[vertex.Key] = int.MaxValue;
}
nodes.Add(vertex.Key);
}
while (nodes.Count != 0)
{
nodes.Sort((x, y) => dist[x] - dist[y]);
var smallest = nodes[0];
nodes.Remove(smallest);
if (smallest == EnemyPos)
{
path = new List<GameObject>();
while (previous.ContainsKey(smallest))
{
path.Add(smallest);
smallest = previous[smallest];
}
break;
}
if (dist[smallest] == int.MaxValue)
{
break;
}
foreach (var neighbor in vertices[smallest])
{
var alt = dist[smallest] + neighbor.Value;
if (alt < dist[neighbor.Key])
{
dist[neighbor.Key] = alt;
previous[neighbor.Key] = smallest;
}
}
}
return path;
}
}
Comment