Anyone know where the error is? and what can be done?

IndexOutOfRangeException: Array index is out of range. FSM.Agent+c__Iterator1.MoveNext () (at Assets/Scripts/FSM/Agent.cs:47) UnityEngine.MonoBehaviour:StartCoroutine(String) FSM.Agent:OnPathFound(Vector3, Boolean) (at Assets/Scripts/FSM/Agent.cs:41) PathRequestManager:FinishedProcessingPath(Vector3, Boolean) (at Assets/Scripts/AStar/PathRequestManager.cs:41) —> this is the error

using UnityEngine;
using System.Collections;

namespace FSM
{
public abstract class Agent : MonoBehaviour
{
private static int agents = 0;

    public Location TargetLocation { get; set; }
    public Location Location { get; set; }

    public float speed = 1;
    Vector3[] path;
    int targetIndex;

    public int ID
    {
        get;
        private set;
    }

    public Agent()
    {
        ID = agents++;
    }

    public void ChangeLocation(Vector3 currentTarget)
    {
        PathRequestManager.RequestPath(transform.position, currentTarget, OnPathFound);
    }

    public void OnPathFound(Vector3[] newPath, bool pathSuccessful)
    {
        if (pathSuccessful)
        {
            path = newPath;

            StopCoroutine("FollowPath");
            StartCoroutine("FollowPath");
        }
    }

    IEnumerator FollowPath()
    {
        Vector3 currentWayPoint = path[0];

        while (true)
        {
            if (transform.position == currentWayPoint)
            {
                targetIndex++;

                if (targetIndex >= path.Length)
                {
                    targetIndex = 0;
                    path = new Vector3[0];
                    yield break;
                }

                currentWayPoint = path[targetIndex];
            }

            currentWayPoint.y = transform.position.y; // 

            transform.position = Vector3.MoveTowards(transform.position, currentWayPoint, speed * Time.deltaTime);
            yield return null;
        }
    }

    public void OnDrawGizmos()
    {
        if (path != null)
        {
            for (int i = targetIndex; i < path.Length; i++)
            {
                Gizmos.color = Color.black;
                Gizmos.DrawCube(path*, Vector3.one);*

if (i == targetIndex)
{
Gizmos.DrawLine(transform.position, path*);*
}
else
{
Gizmos.DrawLine(path[i - 1], path*);*
}
}
}
}
abstract public bool HandleMessage(Telegram telegram);
abstract public bool HandleSense(Sense sense);
}
}

First of all: Don’t use a method called “Agent” inside a class called “Agent” This is interpreted as a constructor!

Please review your code, because it’s really unstable code especially inside the coroutine!

Now to solve your problem, you should check before starting the FollowPath coroutine if the path at least has one value:

using UnityEngine; 
using System.Collections;

namespace FSM
{
    public abstract class Agent : MonoBehaviour
    {
        private static int agents = 0;

        public Location TargetLocation { get; set; }
        public Location Location { get; set; }
        public float speed = 1;
        Vector3[] path;
        int targetIndex;
        public int ID
        {
            get;
            private set;
        }
        public Agent()
        {
            ID = agents++;
        }
        public void ChangeLocation(Vector3 currentTarget)
        {
            PathRequestManager.RequestPath(transform.position, currentTarget, OnPathFound);
        }
        public void OnPathFound(Vector3[] newPath, bool pathSuccessful)
        {
            if (pathSuccessful)
            {
                path = newPath;
                StopCoroutine("FollowPath");
                StartCoroutine("FollowPath");
            }
        }
        IEnumerator FollowPath()
        {
            if (path != null && path.Length > 0)
            {
                Vector3 currentWayPoint = path[0];

                while (true)
                {
                    if (transform.position == currentWayPoint)
                    {
                        targetIndex++;
                        if (targetIndex >= path.Length)
                        {
                            targetIndex = 0;
                            path = new Vector3[0];
                            yield break;
                        }
                        currentWayPoint = path[targetIndex];
                    }
                    currentWayPoint.y = transform.position.y; // 
                    transform.position = Vector3.MoveTowards(transform.position, currentWayPoint, speed*Time.deltaTime);
                    yield return null;
                }
            }
            yield return null;
        }
        public void OnDrawGizmos()
        {
            if (path != null)
            {
                for (int i = targetIndex; i < path.Length; i++)
                {
                    Gizmos.color = Color.black;
                    Gizmos.DrawCube(path*, Vector3.one);*

if (i == targetIndex)
{
Gizmos.DrawLine(transform.position, path*);*
}
else
{
Gizmos.DrawLine(path[i - 1], path*);*
}
}
}
}
abstract public bool HandleMessage(Telegram telegram);
abstract public bool HandleSense(Sense sense);
}