For more context, here’s the full EnemyMover script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Enemy))]
public class EnemyMover : MonoBehaviour
{
[SerializeField] [Range(0f, 5f)]
float speed = 1f;
List path = new List();
Enemy enemy;
GridManager gridManager;
Pathfinder pathfinder;
void OnEnable()
{
ReturnToStart();
RecalculatePath(true);
}
private void Awake()
{
enemy = GetComponent<Enemy>();
gridManager = FindObjectOfType<GridManager>();
pathfinder = FindObjectOfType<Pathfinder>();
}
private void Update()
{
Debug.Log("gridManager: " + gridManager);
Debug.Log("pathfinder: " + pathfinder);
}
void RecalculatePath(bool resetPath)
{
Vector3Int coordinates = new Vector3Int();
if(resetPath)
{
coordinates = pathfinder.StartCoordinates;
}
else
{
coordinates = gridManager.GetCoordinatesFromPosition(transform.position);
}
StopAllCoroutines();
path.Clear();
path = pathfinder.GetNewPath(coordinates);
StartCoroutine(FollowPath());
}
void ReturnToStart()
{
transform.position = gridManager.GetPositionFromCoordinates(pathfinder.StartCoordinates);
}
void FinishPath()
{
enemy.StealGold();
gameObject.SetActive(false);
}
IEnumerator FollowPath()
{
for(int i = 1; i < path.Count; i++)
{
Vector3 startPosition = transform.position;
Vector3 endPosition = gridManager.GetPositionFromCoordinates(path[i].coordinates);
float travelPercent = 0f;
transform.LookAt(endPosition);
while (travelPercent < 1f)
{
travelPercent += Time.deltaTime * speed;
transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
yield return new WaitForEndOfFrame();
}
}
FinishPath();
}
}
The void Update is just a couple of Debug lines I threw in to test the gridManager and pathfinder variables. They are empty, but I’m not sure if they should be showing a value since they are grabbing other scripts.
I don’t have much experience debugging, so I might be missing something basic.