- Home /
NavMeshAgent.CalculatePath, keep getting NullReferenceException
Hi all,
I'm really struggling to get CalculatePath to work. I basically have written this JS code to test it out:
#pragma strict
function Update(){
var navMeshComponent : NavMeshAgent = gameObject.GetComponent(NavMeshAgent);
var path : NavMeshPath;
if(navMeshComponent.CalculatePath(Vector3(-39,1,1),path)){
Debug.Log("I can find a path");
}
}
However, whenever I run this I get this error:
NullReferenceException: Object reference not set to an instance of an object UnityEngine.NavMeshAgent.CalculatePath (Vector3 targetPosition, UnityEngine.NavMeshPath path) RoamingBehaviour.Update ()
What am I doing wrong?
Thanks in advance, Mark
Exact same problem here. Some help would be really useful!
I also receive the same problem. The Nav$$anonymous$$esh and Nav$$anonymous$$eshAgent documentation is really lacking if you want to do anything more advanced than moving the agent around and picking destinations.
Anyone has updates on this? I've spent the last 2 days trying tons of combinations and no luck, always getting a "NullReferenceException: Object reference not set to an instance of an object"
the error in the original post doesn't seem to indicate this, but it may be caused by failure to explicitly typecast, (maybe?...) as in:
var nav$$anonymous$$eshComponent : Nav$$anonymous$$eshAgent = gameObject.GetComponent(Nav$$anonymous$$eshAgent);
(just a longshot)
@Seth Bergman
Yeah that is an error on my part in writing this post, the actual code I have does have the Nav$$anonymous$$eshAgent explicity set.
Answer by mwinteringham · Sep 17, 2012 at 01:33 PM
Found the answer and it is to do with how you create the NavMeshPath variable. In summary adding the following to my problem code above will remove the error:
var path : NavMeshPath = NavMeshPath();
In more detail after some reviewing of the issue, I suspected that the error being produced was related to the path variable being sent to the CalculatePath function as opposed to the CalculatePath function itself. Therefore I tried merely invoking the ClearCorners function attached to a NavMeshPath, which in turn produced the same NullReferenceException.
I then looked over the documentation and found the NavMeshPath has a constructor that needs to be run before you can use the path variable in other functions. So once I invoked the NavMeshPath construtor the Exception error was removed.
http://docs.unity3d.com/Documentation/ScriptReference/NavMeshPath.html
Yes, this solves the issue. Very disappointing that the documentation has a non-working example given:
http://docs.unity3d.com/ScriptReference/Nav$$anonymous$$eshAgent.CalculatePath.html
There is no constructor there.
As of 2018-11-13 the documentation now shows this, the code on https://docs.unity3d.com/ScriptReference/AI.Nav$$anonymous$$eshAgent.CalculatePath.html to init the path variable in C# is Nav$$anonymous$$eshPath path = new Nav$$anonymous$$eshPath();
Answer by wushuang212 · May 28, 2013 at 10:44 AM
I meet the same problem, but the document of Unity doesn't show it. The first time I think the CalcPath() function may be this is right : "function CalculatePath (targetPosition : Vector3, out path : NavMeshPath) : boolean"