[Solved]NavMeshAgent Error (?)

Hi everyone, I receive this error during test sample scene: with 8 other errors in screenshot Any feedback is appreciated be safe everyone and stay healthy, I apologize it is not a Ben a Rick class :slight_smile:

Assets\PortalReignDiscordant\Scripts\NavAgentExample.cs(6,9): error CS0246: The type or namespace name ‘NavMeshAgent’ could not be found (are you missing a using directive or an assembly reference?)

Sounds like there is improper definitions in the NavAgentExample.cs script. Can I see the script in question?

Hi Riley

public bool PathPending = false;
public bool PathStale = false;
public NavMeshPathStatus PathStatus = NavMeshPathStatus.PathInvalid;

// Private Members
private NavMeshAgent _navAgent = null;

// -----------------------------------------------------
// Name :	Start
// Desc	:	Cache MavMeshAgent and set initial 
//			destination.
// -----------------------------------------------------
void Start()
{
	// Cache NavMeshAgent Reference
	_navAgent = GetComponent<NavMeshAgent>();

	// Turn off auto-update
	/*_navAgent.updatePosition = false;
	_navAgent.updateRotation = false;*/


	// If not valid Waypoint Network has been assigned then return
	if (WaypointNetwork == null) return;

	SetNextDestination(false);
}

// -----------------------------------------------------
// Name	:	SetNextDestination
// Desc	:	Optionally increments the current waypoint
//			index and then sets the next destination
//			for the agent to head towards.
// -----------------------------------------------------
void SetNextDestination(bool increment)
{
	// If no network return
	if (!WaypointNetwork) return;

	// Calculatehow much the current waypoint index needs to be incremented
	int incStep = increment ? 1 : 0;

	// Calculate index of next waypoint factoring in the increment with wrap-around and fetch waypoint 
	int nextWaypoint = (CurrentIndex + incStep >= WaypointNetwork.Waypoints.Count) ? 0 : CurrentIndex + incStep;
	Transform nextWaypointTransform = WaypointNetwork.Waypoints[nextWaypoint];

	// Assuming we have a valid waypoint transform
	if (nextWaypointTransform != null)
	{
		// Update the current waypoint index, assign its position as the NavMeshAgents
		// Destination and then return
		CurrentIndex = nextWaypoint;
		_navAgent.destination = nextWaypointTransform.position;
		return;
	}

	// We did not find a valid waypoint in the list for this iteration
	CurrentIndex++;
}

// ---------------------------------------------------------
// Name	:	Update
// Desc	:	Called each frame by Unity
// ---------------------------------------------------------
void Update()
{
	// Copy NavMeshAgents state into inspector visible variables
	HasPath = _navAgent.hasPath;
	PathPending = _navAgent.pathPending;
	PathStale = _navAgent.isPathStale;
	PathStatus = _navAgent.pathStatus;

	// If we don't have a path and one isn't pending then set the next
	// waypoint as the target, otherwise if path is stale regenerate path
	if ((!HasPath && !PathPending) || PathStatus == NavMeshPathStatus.PathInvalid /*|| PathStatus==NavMeshPathStatus.PathPartial*/)
		SetNextDestination(true);
	else
	if (_navAgent.isPathStale)
		SetNextDestination(false);
}

I notice just now the scripts do not have headers like this :
I will add a let you results :
using UnityEngine;
using System.Collections.Generic;

Hi Riley, I added the following lines but these line of code were created in this script Thank you for any feedback :slight_smile:

using UnityEngine;
using System.Collections.Generic;


public class NavMeshExample : MonoBehaviour
{

Lines of code changed after adding above

// If we don't have a path and one isn't pending then set the next
		// waypoint as the target, otherwise if path is stale regenerate path
		if ((!HasPath && !PathPending) || PathStatus == UnityEngine.AI.NavMeshPathStatus.PathInvalid /*|| PathStatus==NavMeshPathStatus.PathPartial*/)
			SetNextDestination(true);
		else

All that code is within a class or just on it’s own? Make sure you got all your closing curling brackets.

It is just on its own I believe I did check my curling brackets will check again. I following a class instructions this one:

Send the full script you have and I’ll see if I can get it sorted out.

Hi Riley, I think you ae super for this :slight_smile:
These are all the scripts I work with.

using UnityEngine;
using System.Collections.Generic;

// Display Mode that the Custom Inspector of an AIWaypointNetwork
// component can be in
public enum PathDisplayMode { None, Connections, Paths }

// -------------------------------------------------------------------
// CLASS	:	AIWaypointNetwork
// DESC		:	Contains a list of waypoints. Each waypoint is a 
//				reference to a transform. Also contains settings
//				for the Custom Inspector
// ------------------------------------------------------------------
public class AIWaypointNetwork : MonoBehaviour 
{		
	[HideInInspector]												
	public PathDisplayMode DisplayMode = PathDisplayMode.Connections;	// Current Display Mode
	[HideInInspector]	
	public int UIStart 	= 0;											// Start wayopoint index for Paths mode
	[HideInInspector]
	public int UIEnd	= 0;											// End waypoint index for Paths mode

	// List of Transform references
	public List<Transform> Waypoints   = new List<Transform>();

}

using UnityEngine;
using System.Collections;
using UnityEditor;

// ------------------------------------------------------------------------------------
// CLASS : AIWaypointNetworkEditor
// DESC : Custom Inspector and Scene View Rendering for the AIWaypointNetwork
// Component
// ------------------------------------------------------------------------------------
[CustomEditor(typeof(AIWaypointNetwork))]
public class AIWaypointNetworkEditor : Editor
{
// --------------------------------------------------------------------------------
// Name : OnInspectorGUI (Override)
// Desc : Called by Unity Editor when the Inspector needs repainting for an
// AIWaypointNetwork Component
// --------------------------------------------------------------------------------
public override void OnInspectorGUI()
{
// Get reference to selected component
AIWaypointNetwork network = (AIWaypointNetwork)target;

	// Show the Display Mode Enumeration Selector
	network.DisplayMode = (PathDisplayMode)EditorGUILayout.EnumPopup ( "Display Mode", network.DisplayMode );

	// If we are in Paths display mode then display the integer sliders for the Start and End waypoint indices
	if (network.DisplayMode==PathDisplayMode.Paths)
	{
		network.UIStart		= EditorGUILayout.IntSlider ( "Waypoint Start" , network.UIStart, 0, network.Waypoints.Count-1);
		network.UIEnd		= EditorGUILayout.IntSlider ( "Waypoint End" , network.UIEnd, 0, network.Waypoints.Count-1);
	}

	// Tell Unity to do its default drawing of all serialized members that are NOT hidden in the inspector
	DrawDefaultInspector();
}


// --------------------------------------------------------------------------------
// Name	:	OnSceneGUI
// Desc	:	Implementing this functions means the Unity Editor will call it when
//			the Scene View is being repainted. This gives us a hook to do our
//			own rendering to the scene view.
// --------------------------------------------------------------------------------
void OnSceneGUI()
{
	// Get a reference to the component being rendered
	AIWaypointNetwork network = (AIWaypointNetwork)target;

	// Fetch all waypoints from the network and render a label for each one
	for(int i=0; i<network.Waypoints.Count;i++)
	{
		if (network.Waypoints[i]!=null)
			Handles.Label ( network.Waypoints[i].position, "Waypoint "+i.ToString ());
	}

	// If we are in connections mode then we will to draw lines
	// connecting all waypoints
	if (network.DisplayMode == PathDisplayMode.Connections)
	{
		// Allocate array of vector to store the polyline positions
		Vector3 [] linePoints = new Vector3[ network.Waypoints.Count+1 ];

		// Loop through each waypoint + one additional interation
		for(int i=0; i<=network.Waypoints.Count;i++)
		{
			// Calculate the waypoint index with wrap-around in the
			// last loop iteration
			int index = i!=network.Waypoints.Count ? i : 0; 

			// Fetch the position of the waypoint for this iteration and
			// copy into our vector array.
			if (network.Waypoints[index]!=null)
				linePoints[i] = network.Waypoints[index].position;
			else
				linePoints[i] = new Vector3(Mathf.Infinity, Mathf.Infinity, Mathf.Infinity);
		}

		// Set the Handle color to Cyan
		Handles.color = Color.cyan;

		// Render the polyline in the scene view by passing in our list of waypoint positions
		Handles.DrawPolyLine ( linePoints );
	}
	else
	// We are in paths mode so to proper navmesh path search and render result
	if (network.DisplayMode == PathDisplayMode.Paths)
	{
		// Allocate a new NavMeshPath
		UnityEngine.AI.NavMeshPath path 	= new UnityEngine.AI.NavMeshPath();

		// Assuming both the start and end waypoint indices selected are ligit
		if (network.Waypoints[network.UIStart]!=null && network.Waypoints[network.UIEnd]!=null)
		{
			// Fetch their positions from the waypoint network
			Vector3 from 		= network.Waypoints[network.UIStart].position;
			Vector3 to			= network.Waypoints[network.UIEnd].position;

			// Request a path search on the nav mesh. This will return the path between
			// from and to vectors
			UnityEngine.AI.NavMesh.CalculatePath ( from, to, UnityEngine.AI.NavMesh.AllAreas, path );

			// Set Handles color to Cyan
			Handles.color = Color.cyan;

			// Draw a polyline passing int he path's corner points
			Handles.DrawPolyLine( path.corners );
		}
	}
	
}

}


	// -----------------------------------------------------
	// Name	:	SetNextDestination
	// Desc	:	Optionally increments the current waypoint
	//			index and then sets the next destination
	//			for the agent to head towards.
	// -----------------------------------------------------
	void SetNextDestination(bool increment)
	{
		// If no network return
		if (!WaypointNetwork) return;

		// Calculatehow much the current waypoint index needs to be incremented
		int incStep = increment ? 1 : 0;

		// Calculate index of next waypoint factoring in the increment with wrap-around and fetch waypoint 
		int nextWaypoint = (CurrentIndex + incStep >= WaypointNetwork.Waypoints.Count) ? 0 : CurrentIndex + incStep;
		Transform nextWaypointTransform = WaypointNetwork.Waypoints[nextWaypoint];

		// Assuming we have a valid waypoint transform
		if (nextWaypointTransform != null)
		{
			// Update the current waypoint index, assign its position as the NavMeshAgents
			// Destination and then return
			CurrentIndex = nextWaypoint;
			_navAgent.destination = nextWaypointTransform.position;
			return;
		}

		// We did not find a valid waypoint in the list for this iteration
		CurrentIndex++;
	}

	// ---------------------------------------------------------
	// Name	:	Update
	// Desc	:	Called each frame by Unity
	// ---------------------------------------------------------
	void Update()
	{
		// Copy NavMeshAgents state into inspector visible variables
		HasPath = _navAgent.hasPath;
		PathPending = _navAgent.pathPending;
		PathStale = _navAgent.isPathStale;
		PathStatus = _navAgent.pathStatus;

		// If we don't have a path and one isn't pending then set the next
		// waypoint as the target, otherwise if path is stale regenerate path
		if ((!HasPath && !PathPending) || PathStatus == UnityEngine.AI.NavMeshPathStatus.PathInvalid /*|| PathStatus==NavMeshPathStatus.PathPartial*/)
			SetNextDestination(true);
		else
		if (_navAgent.isPathStale)
			SetNextDestination(false);
	}
}


Okay I think my next question is all this code in a single file or seperate files?

I apologize for that will separate got a phone call from client had to stop what I was doing hold on.

using UnityEngine;
using System.Collections.Generic;

// Display Mode that the Custom Inspector of an AIWaypointNetwork
// component can be in
public enum PathDisplayMode { None, Connections, Paths }

// -------------------------------------------------------------------
// CLASS	:	AIWaypointNetwork
// DESC		:	Contains a list of waypoints. Each waypoint is a 
//				reference to a transform. Also contains settings
//				for the Custom Inspector
// ------------------------------------------------------------------
public class AIWaypointNetwork : MonoBehaviour 
{		
	[HideInInspector]												
	public PathDisplayMode DisplayMode = PathDisplayMode.Connections;	// Current Display Mode
	[HideInInspector]	
	public int UIStart 	= 0;											// Start wayopoint index for Paths mode
	[HideInInspector]
	public int UIEnd	= 0;											// End waypoint index for Paths mode

	// List of Transform references
	public List<Transform> Waypoints   = new List<Transform>();

}

using UnityEngine;
using System.Collections;
using UnityEditor;

// ------------------------------------------------------------------------------------
// CLASS	:	AIWaypointNetworkEditor
// DESC		:	Custom Inspector and Scene View Rendering for the AIWaypointNetwork
//				Component
// ------------------------------------------------------------------------------------
[CustomEditor(typeof(AIWaypointNetwork))]
public class AIWaypointNetworkEditor : Editor 
{
	// --------------------------------------------------------------------------------
	// Name	:	OnInspectorGUI (Override)
	// Desc	:	Called by Unity Editor when the Inspector needs repainting for an
	//			AIWaypointNetwork Component
	// --------------------------------------------------------------------------------
	public override void OnInspectorGUI()
	{
		// Get reference to selected component
		AIWaypointNetwork network = (AIWaypointNetwork)target;
	
		// Show the Display Mode Enumeration Selector
		network.DisplayMode = (PathDisplayMode)EditorGUILayout.EnumPopup ( "Display Mode", network.DisplayMode );
	
		// If we are in Paths display mode then display the integer sliders for the Start and End waypoint indices
		if (network.DisplayMode==PathDisplayMode.Paths)
		{
			network.UIStart		= EditorGUILayout.IntSlider ( "Waypoint Start" , network.UIStart, 0, network.Waypoints.Count-1);
			network.UIEnd		= EditorGUILayout.IntSlider ( "Waypoint End" , network.UIEnd, 0, network.Waypoints.Count-1);
		}

		// Tell Unity to do its default drawing of all serialized members that are NOT hidden in the inspector
		DrawDefaultInspector();
	}


	// --------------------------------------------------------------------------------
	// Name	:	OnSceneGUI
	// Desc	:	Implementing this functions means the Unity Editor will call it when
	//			the Scene View is being repainted. This gives us a hook to do our
	//			own rendering to the scene view.
	// --------------------------------------------------------------------------------
	void OnSceneGUI()
	{
		// Get a reference to the component being rendered
		AIWaypointNetwork network = (AIWaypointNetwork)target;

		// Fetch all waypoints from the network and render a label for each one
		for(int i=0; i<network.Waypoints.Count;i++)
		{
			if (network.Waypoints[i]!=null)
				Handles.Label ( network.Waypoints[i].position, "Waypoint "+i.ToString ());
		}

		// If we are in connections mode then we will to draw lines
		// connecting all waypoints
		if (network.DisplayMode == PathDisplayMode.Connections)
		{
			// Allocate array of vector to store the polyline positions
			Vector3 [] linePoints = new Vector3[ network.Waypoints.Count+1 ];

			// Loop through each waypoint + one additional interation
			for(int i=0; i<=network.Waypoints.Count;i++)
			{
				// Calculate the waypoint index with wrap-around in the
				// last loop iteration
				int index = i!=network.Waypoints.Count ? i : 0; 

				// Fetch the position of the waypoint for this iteration and
				// copy into our vector array.
				if (network.Waypoints[index]!=null)
					linePoints[i] = network.Waypoints[index].position;
				else
					linePoints[i] = new Vector3(Mathf.Infinity, Mathf.Infinity, Mathf.Infinity);
			}

			// Set the Handle color to Cyan
			Handles.color = Color.cyan;

			// Render the polyline in the scene view by passing in our list of waypoint positions
			Handles.DrawPolyLine ( linePoints );
		}
		else
		// We are in paths mode so to proper navmesh path search and render result
		if (network.DisplayMode == PathDisplayMode.Paths)
		{
			// Allocate a new NavMeshPath
			UnityEngine.AI.NavMeshPath path 	= new UnityEngine.AI.NavMeshPath();

			// Assuming both the start and end waypoint indices selected are ligit
			if (network.Waypoints[network.UIStart]!=null && network.Waypoints[network.UIEnd]!=null)
			{
				// Fetch their positions from the waypoint network
				Vector3 from 		= network.Waypoints[network.UIStart].position;
				Vector3 to			= network.Waypoints[network.UIEnd].position;

				// Request a path search on the nav mesh. This will return the path between
				// from and to vectors
				UnityEngine.AI.NavMesh.CalculatePath ( from, to, UnityEngine.AI.NavMesh.AllAreas, path );

				// Set Handles color to Cyan
				Handles.color = Color.cyan;

				// Draw a polyline passing int he path's corner points
				Handles.DrawPolyLine( path.corners );
			}
		}
		
	}

}
using UnityEngine;
using System.Collections.Generic;


public class NavMeshExample : MonoBehaviour
{

	public bool PathPending = false;
	public bool PathStale = false;
	public UnityEngine.AI.NavMeshPathStatus PathStatus = UnityEngine.AI.NavMeshPathStatus.PathInvalid;

	// Private Members
	private UnityEngine.AI.NavMeshAgent _navAgent = null;

	// -----------------------------------------------------
	// Name :	Start
	// Desc	:	Cache MavMeshAgent and set initial 
	//			destination.
	// -----------------------------------------------------
	void Start()
	{
		// Cache NavMeshAgent Reference
		_navAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();

		// Turn off auto-update
		/*_navAgent.updatePosition = false;
		_navAgent.updateRotation = false;*/


		// If not valid Waypoint Network has been assigned then return
		if (WaypointNetwork == null) return;

		SetNextDestination(false);
	}

	// -----------------------------------------------------
	// Name	:	SetNextDestination
	// Desc	:	Optionally increments the current waypoint
	//			index and then sets the next destination
	//			for the agent to head towards.
	// -----------------------------------------------------
	void SetNextDestination(bool increment)
	{
		// If no network return
		if (!WaypointNetwork) return;

		// Calculatehow much the current waypoint index needs to be incremented
		int incStep = increment ? 1 : 0;

		// Calculate index of next waypoint factoring in the increment with wrap-around and fetch waypoint 
		int nextWaypoint = (CurrentIndex + incStep >= WaypointNetwork.Waypoints.Count) ? 0 : CurrentIndex + incStep;
		Transform nextWaypointTransform = WaypointNetwork.Waypoints[nextWaypoint];

		// Assuming we have a valid waypoint transform
		if (nextWaypointTransform != null)
		{
			// Update the current waypoint index, assign its position as the NavMeshAgents
			// Destination and then return
			CurrentIndex = nextWaypoint;
			_navAgent.destination = nextWaypointTransform.position;
			return;
		}

		// We did not find a valid waypoint in the list for this iteration
		CurrentIndex++;
	}

	// ---------------------------------------------------------
	// Name	:	Update
	// Desc	:	Called each frame by Unity
	// ---------------------------------------------------------
	void Update()
	{
		// Copy NavMeshAgents state into inspector visible variables
		HasPath = _navAgent.hasPath;
		PathPending = _navAgent.pathPending;
		PathStale = _navAgent.isPathStale;
		PathStatus = _navAgent.pathStatus;

		// If we don't have a path and one isn't pending then set the next
		// waypoint as the target, otherwise if path is stale regenerate path
		if ((!HasPath && !PathPending) || PathStatus == UnityEngine.AI.NavMeshPathStatus.PathInvalid /*|| PathStatus==NavMeshPathStatus.PathPartial*/)
			SetNextDestination(true);
		else
		if (_navAgent.isPathStale)
			SetNextDestination(false);
	}
}

Thank you :slight_smile:

Hi Riley, I was able to fix some lines of error this is was left:

Hi Riley, I removed the duplicate scripts posted in other folders unity creates that solved some errors. I also removed the NavAgentExample script this solved all errors. I will start from scratch with the NavAgentExample script maybe I can pinpoint issues. Thank you