# Unity NavMesh pathfinding using Job System

I'm trying to write a system where I can run each enemy's NavMesh pathfinding in parallel, using Job System.

The problem I'm having is that I can't pass the NavMesh agent argument as it's a reference type.

InvalidOperationException: PathfindJob.agent is not a value type. Job structs may not contain any reference types.

I'm probably approaching this problem in a completely wrong way, but this is all I've come up with.

Here's the current Enemy code

using System.ComponentModel;
using Managers;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Jobs;

namespace Characters
{
[RequireComponent(typeof(Rigidbody))]
public class Enemy : MonoBehaviour
{
[Header("Stats"), SerializeField] private int _health = 100;
[SerializeField] private int _damage = 20;
[SerializeField] private float _aggroRadius = 10f;

private Rigidbody _body;
private NavMeshAgent _agent;
private Transform _target;

private void Awake()
{
_body = GetComponent<Rigidbody>();
_agent = GetComponent<NavMeshAgent>();
_target = PlayerManager.GetInstance().GetPlayer();
}

private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
}
}

public struct PathfindJob : IJobParallelForTransform
{
public Vector3 TargetPosition;
public NavMeshAgent Agent;

public void Execute(int index, TransformAccess transform)
{
var distance = Vector3.Distance(TargetPosition, transform.position);

Agent.SetDestination(TargetPosition);
}
}
}


I'm scheduling the job from the GameManager class.

So, IIRC the only thing that you are doing in parallel with this is calculating the distance and setting the target. The actual Agent calculations happen in a different thread within the NavMesh system. All you are doing is (re)setting the NavMesh agent target over and over again - basically just passing in a Vector3 over and over.

What you (presumably) want is NavMesh actually be implemented in ECS, which is something entirely different. You could absolutely implement your own A* system in ECS as an exercise, but I suspect that won't get you closer to making a game.

Here is a video that does a good job showing the limits/capabilities of NavMesh agents without ECS.

Alternatively you could write a job that would tag all agents that should start navigating somewhere else and use a hybrid system to fetch those tagged entities and execute the SetDestination method with the data from the tag component.