Here is the error
Script
using UnityEngine;
using System.Collections;
using Pathfinding;
using System.Collections.Generic;
public class EnemyUnit : MonoBehaviour
{
public GameObject EnemySpawnLocations;
private Seeker seeker;
private CharacterController controller;
public Path path;
private Unit unit;
public float speed;
public float defaultnextWaypointDistance = 3f;
public int currentWaypoint = 0;
public bool walking = false;
void Update()
{
GameObject world = GameObject.Find("World");
float common = world.GetComponent<Common>().timer;
if(!walking Common > 10)
{
StartCoroutine(Walk());
}
}
void Start()
{
seeker = GetComponent<Seeker> ();
controller = GetComponent<CharacterController> ();
unit = GetComponent<Unit> ();
}
//Find Closest Enemy
#region FindClosestEnemy
public GameObject FindClosestEnemy()
{
GameObject[] Units;
Units = GameObject.FindGameObjectsWithTag("Unit");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = this.transform.position;
foreach (GameObject go in Units)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
#endregion
public IEnumerator Walk()
{
if(unit.transform.position != FindClosestEnemy().transform.position);
{
walking = true;
Debug.Log (FindClosestEnemy().transform.position);
yield return new WaitForSeconds(5.0F);
seeker.StartPath (transform.position, FindClosestEnemy().transform.position, OnPathComplete);
yield return new WaitForSeconds(5.0F);
walking = false;
}
}
// Pathfinding logic
#region PathFinding
public void OnPathComplete(Path p)
{
if(!p.error)
{
path = p;
// reset waypoint
currentWaypoint = 0;
}
}
public void FixedUpdate()
{
if (!unit.isWalkable)
return;
if (path == null)
return;
if (currentWaypoint >= path.vectorPath.Count)
return;
// Calculate the direction of the unit
Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;
controller.SimpleMove (dir); //UNIT MOVEEES!!!
transform.LookAt(new Vector3(path.vectorPath[currentWaypoint].x, transform.position.y, path.vectorPath[currentWaypoint].z));
float nextWaypointDistance = defaultnextWaypointDistance;
if (currentWaypoint == path.vectorPath.Count - 1)
nextWaypointDistance = 0f;
//Check if close enough to the current way point if we are progress to next waypoint
if (Vector3.Distance (transform.position, path.vectorPath [currentWaypoint]) < nextWaypointDistance)// Defined At top
{
currentWaypoint++;
return;
}
}
#endregion
}