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 /
avatar image
0
Question by Harker · Aug 18, 2015 at 08:26 PM · c#stackdijkstra

Dijkstra Algorithm, ArgumentNullException: Argument cannot be null.

Hi I am trying to implement Dijkstras Algorithm as a basic pathfinding mathod on an chessboard like grid. I want place a block at every tile I used for a path between two tiles given by the user. I got it working so far, but when I try to use every tile just once, so it is blocked for every future path, I get this Error:

 ArgumentNullException: Argument cannot be null.
 Parameter name: key
 System.Collections.Generic.Dictionary`2[UnityEngine.GameObject,System.Single].get_Item (UnityEngine.GameObject key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:136)
 DijkstraAlgorithm.Dijkstra (UnityEngine.GameObject[,] grid, UnityEngine.GameObject start, UnityEngine.GameObject end) (at Assets/Scripts/DijkstraAlgorithm.cs:50)
 MouseScript.Update () (at Assets/Scripts/MouseScript.cs:41)

This would be my code:

 static public Stack<GameObject> Dijkstra(GameObject[,]grid, GameObject start, GameObject end)
     {
         Dictionary<GameObject, float> dist = new Dictionary<GameObject, float>();
         Dictionary<GameObject, GameObject> previous = new Dictionary<GameObject, GameObject>();
         List<GameObject> Q = new List<GameObject>();
         
         foreach(GameObject v in grid)
         {
             dist[v] = Mathf.Infinity;
             previous[v] = null;
             Q.Add(v);
         }
 
         dist[start] = 0;
         while(Q.Count > 0)
         {
             float shortestDistance = Mathf.Infinity;
             GameObject shortestDistanceNode = null;
             foreach(GameObject obj in Q)
             {
                 if (dist[obj] < shortestDistance )
                 {
                     shortestDistance = dist[obj];
                     shortestDistanceNode = obj;
                 }
                 
             }
 
             GameObject u = shortestDistanceNode;
 
             Q.Remove(u);
             if(u==end)
             {
                 Stack<GameObject> stack = new Stack<GameObject>();
                 while(previous[u] != null)
                 {
                     stack.Push(u);
                     u = previous[u];
                 }
                 return stack;
             }
 
             if(dist[u] == Mathf.Infinity)
             {
                 break;
             }
 
             foreach(GameObject v in u.GetComponent<Node>().neighbors)
             {
                 if(v.GetComponent<Node>().getBlocked()==false)
                 {
                     break;
                 }
                 float alt = dist[u] + (u.transform.position - v.transform.position).magnitude;
                 if(alt< dist[v])
                 {
                     dist[v] = alt;
                     previous[v] = u;
                 }
             }
 
         }
         return null;
         
     }
 }

I hope someone here can help me, googleing the error has not produced anything helpfull. Thank you in advance Greets Harker

Comment
Add comment · Show 2
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
avatar image cjdev · Aug 19, 2015 at 06:00 AM 0
Share

Looks like you're trying to use a null GameObject as a key in the dictionary. What line is that error on in your code?

avatar image Harker · Aug 19, 2015 at 08:14 AM 0
Share

The error appears when i add this line: if(v.GetComponent<Node>().getBlocked()==false)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CaKeMeaT · Aug 22, 2015 at 12:08 AM

if(v.GetComponent() != null && v.GetComponent().getBlocked()==false)

Comment
Add comment · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C#/Unity Stack size and memory leaks 5 Answers

Android blank screen when can't connect to server 0 Answers

How to ''stack'' coroutines, and call each one till all are executed? 5 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