Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Score, Enemies, and Game State Create Enemy Navigation and AI

Josh Campbell
Josh Campbell
4,463 Points

The name 'NavMeshAgent' does not exist in the current context

I've followed the directions in the video twice now and I still cannot figure out what the issue is. My bird game object has a NavMeshAgent component and I've followed the directions to bake the Nav Mesh in the environment object, but when I go to write the code and declare my NavMeshAgent variable, I get the above message. I've done everything I can think of and my code matches that in the example exactly, but I still get the same issue.

Josh Campbell
Josh Campbell
4,463 Points

I actually ended up figuring this out, so I thought I'd leave this comment for anyone else who runs into the problem. I closed Unity/MonoDevelop and re-opened them and when I did, private NavMeshAgent had been changed to private UnityEngine.AI.NavMeshAgent. So, to fix the problem just add the using UnityEngine.AI namespace and all should be well.

Sean Hernandez
Sean Hernandez
Courses Plus Student 9,350 Points

Thanks Josh this answer was spot on. Much appreciated!!

4 Answers

Also had this problem, you fixed it for me. Thanks!

Jon Kealy
Jon Kealy
9,082 Points

An easier solution would be to right click on "NavMeshAgent" highlighted in red and hover over resolve. This brings up the option to add UnityEngine.AI. Select it and the screen will refresh and 'using UnityEngine.AI' will appear near the top.

Chase Grijalva
Chase Grijalva
255 Points

I had this problem too and found the very same solution.

Patrick Bowles
Patrick Bowles
14,993 Points

I found that adding "using UnityEngine.AI;" at the top of the script file also works.