- Home /
Duplicate Question
Object reference not set to instance of object - Can't find solution.
Hi, I've been having a nightmare trying to get this script sorted out and it's slowly going in the right direction, but when I try and solve the path, for some reason I get the following error:
NullReferenceException: Object reference not set to an instance of an object AStarSolver.GetNodeWithLowestTotal (System.Collections.Generic.List`1 list) (at Assets/Scripts/AStarSolver.cs:126) AStarSolver.Solve (Vector3 startPos, Vector3 endPos) (at Assets/Scripts/AStarSolver.cs:52) DrawPaths.Update () (at Assets/Scripts/DrawPaths.cs:24)
I know that this means I'm trying to access something that hasn't been set, but I don't know why this is.
I'd appreciate it if someone could read it and tell me what's up with it.
http://pastebin.com/74iywYsn the pathfinder.
EDIT: I also want to point out that the grid is generated before anything else is ran, and it still errors out.
you so fast. yeah, an index:
for ( int i = 0; i < list.Count; i++ ) {
openNode = list[i];
Didn't know that was called an index :P Thought that was just called a for loop
Answer by Loius · Apr 10, 2013 at 08:42 PM
change the foreach @ ~126 to an index for and see if that helps. // nevermind, should be fine. foreach (x in y) essentially makes x read-only, but you're not writing so shouldn't be a problem.
also GetNode will gleefully return null with no message, and vector comparisons can be inexact. since you're .Add-ing the result of GetNode with no test for null, it's very possible to be .Add-ing nulls into the list.
And by the way, the vectors are rounded to floor(so anything with a decimal, even ones above .5 will be rounded downwards so that the vectors will match)
but what if there isn't a node there at all?
i'd say just add a debug.log at the end of that function - it should never return null; that's a case of 'I can't do what you asked'
hmm ive changed the return null to return a new node for now. Any idea how I can make it check for the closest node to a point? Like if I have a node at (300,0,50) and I try and get a node with (500,0,60), it would return the closest node: (300,0,50) for example? assu$$anonymous$$g that the (300,0,50) is the closest node
Follow this Question
Related Questions
Make a flying enemy ? 0 Answers
Making my own navmesh 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Link two nodes with a edge 1 Answer