My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMover : MonoBehaviour
{
[SerializeField] List<Waypoint> path = new List<Waypoint>();
[SerializeField] [Range(0.0f, 5.0f)] float speed = 1f;
Enemy enemy;
// Start is called before the first frame update
void OnEnable()
{
FindPath();
ReturnToStart();
StartCoroutine(FollowPath());
}
void Start()
{
enemy = GetComponent<Enemy>();
}
void FindPath()
{
path.Clear();
GameObject parent = GameObject.FindGameObjectWithTag("Path");
foreach(Transform child in parent.transform)
{
path.Add(child.GetComponent<Waypoint>());
}
}
void ReturnToStart()
{
transform.position = path[0].transform.position;
}
IEnumerator FollowPath()
{
foreach(Waypoint waypoint in path)
{
Vector3 startPosition = this.transform.position;
Vector3 endPosition = waypoint.transform.position;
float travelPercent = 0.0f;
transform.LookAt(endPosition);
while(travelPercent < 1.0f)
{
travelPercent += Time.deltaTime * speed;
transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
yield return new WaitForEndOfFrame();
}
}
enemy.StealGold();
gameObject.SetActive(false);
}
}
I don’t know why this error is coming up.