Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mwinteringham · Aug 14, 2012 at 03:12 PM · navmeshnavmeshagent

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

Comment
Add comment · Show 5
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 castor · Sep 15, 2012 at 02:12 AM 0
Share

Exact same problem here. Some help would be really useful!

avatar image justinl · Sep 15, 2012 at 05:17 PM 0
Share

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.

avatar image castor · Sep 17, 2012 at 04:53 AM 0
Share

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"

avatar image Seth-Bergman · Sep 17, 2012 at 06:08 AM 0
Share

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)

avatar image mwinteringham · Sep 17, 2012 at 09:23 AM 0
Share

@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.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 2 · 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
avatar image Madeck · Sep 29, 2014 at 07:41 AM 0
Share

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.

avatar image nicmarxp · Nov 13, 2018 at 07:43 PM 0
Share

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();

avatar image
0

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"

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
avatar image
0

Answer by alii_nezamii · Nov 19, 2016 at 03:05 PM

NavMeshPath path=GetComponent().path;

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

14 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

Related Questions

How can i assign multiple navmeshagents in one script ? 1 Answer

NavMesh flee. Ai flee from player. 4 Answers

Navmesh problem with characters rotation in the terrain 1 Answer

NavMesh Agent rotates on X axis when moving and snaps back to 0. 2 Answers

Tank not moving towards player 1 Answer


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