Hi, I’m creating a kind of tower defense game and this is my script for moving an enemy. It’s attached to an enemy that gets spawned after a path is created. It gets List from PathCreator script on start and only there, the list is also private.
So in my mind - After spawning an enemy, he gets a path and he follows this path even if PathCreator creates a new one because I assign it only on Start.
This is how I want it to work, but for some reason when I create a new path in PathCreator enemy follows it instead of following the one he got on Start().
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyMover : MonoBehaviour
{
[HideInInspector] public Grid.Sides side;
List<Waypoint> path = new List<Waypoint>();
NavMeshAgent agent;
int currentwaypoint = 1;
private void Start()
{
path = FindObjectOfType<PathCreator>().GetPath(side);
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
Move();
}
private void Move()
{
if (currentwaypoint == path.Count)
{
Destroy(gameObject);
return;
}
agent.destination = path[currentwaypoint].transform.position;
path[currentwaypoint].SetColor(Grid.Sides.top, Color.black);
if (IsDestinationReached())
{
SetNextWaypoint();
}
}
private void SetNextWaypoint()
{
currentwaypoint += 1;
}
private bool IsDestinationReached()
{
if (Vector3.Distance(transform.position, agent.destination) < 1.3f) return true;
return false;
}
}
public List<Waypoint> GetPath(Grid.Sides getPathSide)
{
if (getPathSide == Grid.Sides.top)
{
return topPath;
}
if (getPathSide == Grid.Sides.bottom)
{
return bottomPath;
}
return null;
}
This bug isn’t going to affect my game in any way because I’m not going to allow new paths get created after enemies start spawning but I’m curious why it’s working like that and I’d like to understand it.