c# – Passing Variables to a Job in Unity?


I am trying to learn the Job System in Unity, but I can’t seem to pass variables to a Job? I may not be using Jobs correctly, but I am trying to multi-thread my code. The code works without using Jobs. For this, a Job should be created for each game object with the attack tag to find the closest one.

This is the important parts of what I have:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.AI;
using Unity.Mathematics;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Burst;

public class UnitController : MonoBehaviour
{
public string attackTag;
private GameObject() enemies;

void Update()
{
findCurrentTarget();
}

public void findCurrentTarget()
{
        enemies = GameObject.FindGameObjectsWithTag(attackTag);
        float closestDistance = Mathf.Infinity;
   
        NativeList<JobHandle> TargetList = new NativeList<JobHandle>(Allocator.Temp);
        foreach (GameObject potentialTarget in enemies)
        {
            JobHandle Target = findCurrentTargetJob();
            TargetList.Add(Target);
        }
}

public JobHandle findCurrentTargetJob()
{
        FindTargetJob job = new FindTargetJob();
        return job.Schedule();
}

void LateUpdate()
{
        JobHandle.CompleteAll(TargetList);
        TargetList.Dispose();
}
}

(BurstCompile)
public struct FindTargetJob : IJob
{
    public void Execute()
    {
        if (Vector3.Distance(transform.position, potentialTarget.transform.position) < closestDistance && potentialTarget != null)
        {
            closestDistance = Vector3.Distance(transform.position, potentialTarget.transform.position);
            if (!currentTarget || (currentTarget && Vector3.Distance(transform.position, currentTarget.position) > 2))
            {
                if (potentialTarget.transform != gameObject.transform)
                {
                    currentTarget = potentialTarget.transform;
                }
            }
        }
    }
}

I get an error for every variable that I am trying to pass/use:

UnitController.cs(241,24): error CS0103: The name 'transform' does not exist in the current context
UnitController.cs(241,44): error CS0103: The name 'potentialTarget' does not exist in the current context
UnitController.cs(241,82): error CS0103: The name 'closestDistance' does not exist in the current context
UnitController.cs(244,4): error CS0103: The name 'closestDistance' does not exist in the current context

It also does not like the complete and dispose of TargetList in LateUpdate:

UnitController.cs(226,25): error CS0103: The name 'TargetList' does not exist in the current context

Any help would be appreciated!