Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem referencing an int inside of a dictionary. 0 Answers

Trouble with SceneMananger.sceneLoaded and passing dictionaries 0 Answers

NullReferenceException being thrown when I try to add a component to a dictionary 0 Answers

Check if other.object changes his tag (OnTriggerStay2D) 0 Answers

NullReferenceException: A null value was found where an object instance was required. at InitScript.LoginCallback2 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges