index was out of range List

Hey,

I’m looking for some help. For some reason my code just won’t work.
I’m tyring to update the path list of the minion AI. It tells my my index was out of range and i ahve no clue why.
Wavemanager:

    private void SpawnUnit(GameObject prefab, int spawnLoc)
        {
            GameObject go = Instantiate(prefab, 
                            SpawnPoints[spawnLoc].transform.position, 
                            Quaternion.identity);
            Minion minion = go.GetComponent<Minion>(); 
            switch (spawnLoc)
            {
                case 0:
                    {
                        minion.Path = MidSpawnPoints; 
                        minion.Target = MidSpawnPoints[0].transform;
                        break; 
                    }
                case 1:
                    {
                        minion.Path = BotSpawnPoints;
                        minion.Target = BotSpawnPoints[0].transform;
                        break;
                    }
                case 2:
                    {
                        minion.Path = TopSpawnPoints;
                        minion.Target = TopSpawnPoints[0].transform;
                        break;
                    }

**Minon script**


 public class Minion : MonoBehaviour
    {
        public CharacterController Character;
        public NavMeshAgent agent;
        public Transform Target;
        public List<GameObject> Path;
        public int PathCount = 0; 
        // Start is called before the first frame update
        void Start()
        {
            
            agent = GetComponent<NavMeshAgent>();
            agent.updateRotation = false;
            agent.updatePosition = true;
        }
    
        // Update is called once per frame
        private void Update()
        {
            if (Target != null)
            {
                agent.SetDestination(Target.position);
            }
            else
            {
                Target = Path[PathCount].transform;
                PathCount++;
            }
           
    
        }

Look at this part of your code:

     {
         Target = Path[PathCount].transform;
         PathCount++;
     }

and change it like that:

     {
         Target = Path[PathCount].transform;
         PathCount++;
         PathCount%=Path.Count;
     }