0

i am getting the error null reference exception: object reference not set to an instance of an object. i believe it is because my player pos and anemy pos are null but i don't understand why they would be null. sorry for repost as i have seen this else where but couldn't manage to figure it out

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++;


    }


}

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;
    }

}
Tom Unsworth
  • 17
  • 1
  • 7
  • could it be that `g.shortest_path(PlayerPos, EnemyPos).ForEach(x => Debug.Log(x));` is being called before the `OnTriggerEnter2D(Collider2D other)` assigns a value to both objects? – Arno Dec 05 '17 at 13:37
  • I am calling my on trigger enter function before my shortest path function so I don't think it is the order. – Tom Unsworth Dec 05 '17 at 14:17
  • actually i have just been trying to diagnose my problem and it is that the main function containing g.shortest_path(PlayerPos, EnemyPos).ForEach(x => Debug.Log(x)); is being called before the on trigger function but i don't understand why as it is lower down in the code – Tom Unsworth Dec 05 '17 at 14:30
  • if you debug it in visual studio. it should stop on the line where it throws an Exception right. If you open your Call Stack(crtl+q then search for Call Stack) it should show you exacly through what methods you got to the place where you crashed – Arno Dec 05 '17 at 14:58
  • its the g.shortest_path(PlayerPos, EnemyPos).ForEach(x => Debug.Log(x)); line because they are null but i don't understand why the trigger function should run before that line – Tom Unsworth Dec 05 '17 at 15:48
  • Is your Start() method ever called? Maybe when you start your app you end up in the Main() method which is just a few if statements that you don't have to trigger and ends in `g.shortest_path(PlayerPos, EnemyPos).ForEach(x => Debug.Log(x));` – Arno Dec 05 '17 at 15:52
  • yes the main function is running before the start function but i dont need to call the start function because it is running that when i start my programme – Tom Unsworth Dec 05 '17 at 21:18
  • it was my .ForEach(x => Debug.Log(x)); as it was in another class this was not working and resulting in the error. thank you very much for the help – Tom Unsworth Dec 05 '17 at 21:35

0 Answers0