IndexOutOfRangeException: Array index is out of range.

Hey all! So halfway into development of this game, I got this error that I just can’t solve. I’m super new to C# as well so I might have written some unnecessary super long codes that could have been done in a single line. I’m getting there… hopefully.
I’ve been tested for a day. sometime it run perfectly but sometime it gaves me errors. But i guess, there is nothing wrong for code. What i did is this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Unit : MonoBehaviour {

	public GameObject Abintang;
	public Pathfinding pathfinding;

	public Transform target;
	private float speed = 5;
	private Vector3[] path;
	private int targetIndex;
	private bool checkSet;


	public bool pathPending{
		get{ 
			return pathfinding.requestManager.isProcessingPath;
			//return m_pathPending;
		}
	}

	public bool hasPath{
		get{
			return pathfinding.m_hasPath;
		}
	}

	public Vector3 destination{
		get{ 
			if (pathfinding.requestManager.isProcessingPath){
				return pathfinding.m_destination;
			}
			if (path.Length == 0){
				return transform.position;
			}
			if (pathfinding.grid == null){
				return Vector3.positiveInfinity;
			}
			return pathfinding.m_destination;
		}
		set{ 
			if (target != null) {
				pathfinding.m_destination = target.position;
			}
			pathfinding.m_destination = value;
		}
	}

	public float remainingDistance{
		get{ 
			if (pathfinding.m_destination != null){
				return Vector3.Distance( pathfinding.m_destination , transform.position );
			}
		}
	}


	public bool isStopped{
		get{
			return pathfinding.m_isStopped;
		}
		set{ 
			pathfinding.m_isStopped = value;
		}
	}

	public float stoppingDistance { 
		get {
			return pathfinding.m_stoppingDistance;
		} 
		set {
			if (isStopped) {
				pathfinding.m_stoppingDistance = Vector3.Distance (pathfinding.m_destination, transform.position);
			}
		}
	}

	void Start(){
		pathfinding = Abintang.GetComponent<Pathfinding> ();
		//PathRequestManager.RequestPath (transform.position, target.position, OnPathFound);
	}

	void Update(){
		if (checkSet) {
			PathRequestManager.RequestPath (transform.position, pathfinding.m_destination , OnPathFound);
		}


		if (target != null) {
			if (Vector3.Distance (transform.position, target.position) > 1f) {
				SetDestination (target.position);
			}
		}
	}

	public void OnPathFound(Vector3[] newPath,bool pathSuccessful){
		if (pathSuccessful) {
			path = newPath;
			Debug.Log ("Path Length = " + path.Length.ToString ());
			StopCoroutine ("FollowPath");
			StartCoroutine ("FollowPath");
		}
	}

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

		while (true) {
			if (transform.position == currentWaypoint) {
				targetIndex++;
				Debug.Log ("Target Index = " + targetIndex.ToString ());
				if (targetIndex >= path.Length) {
					yield break;
				}
				currentWaypoint = path [targetIndex];
			}
			transform.LookAt (currentWaypoint);
			print ("current waypoint : " + currentWaypoint.ToString ());
			transform.position = Vector3.MoveTowards (transform.position, currentWaypoint, speed * Time.deltaTime);
			checkSet = false;
			//Debug.Log ("CheckSet = False");

			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 );
* }
}
}
}*_

* public bool SetDestination( Vector3 target){
if (target != null) {
pathfinding.m_destination = target;*
* checkSet = true;*

* Debug.Log (“CheckSet = true”);_
print ("SetDestination = " + target.ToString());*
* return true;
} else {
checkSet = false;
return false;
}
}*_

* public void Stop(){*
* StopCoroutine (“FollowPath”);*
* pathfinding.m_isStopped = true;
_ }*_

* public void Resume(){*
* PathRequestManager.RequestPath (transform.position, pathfinding.m_destination, OnPathFound);
_ //StopCoroutine (“FollowPath”);
//StartCoroutine (“FollowPath”);_
pathfinding.m_isStopped = false;
_ }
public void HasReachedDestination(){_
if (Vector3.Distance (transform.position, pathfinding.m_destination) < 0.10f) {
_ checkSet = false;
Debug.Log (“CheckSet = False”);
}
}*_

* public void ResetPath(){*
* System.Array.Resize (ref path, 0);*
* }*
}

and the error point to this line
Vector3 currentWaypoint = path [0];
Thanks for taking out the time to help me out! :slight_smile:

Add the following lines as the first lines of the FollowTarget function in order to avoid the error

 IEnumerator FollowPath(){
     if( path.Length == 0 )
          yield break ;

     Vector3 currentWaypoint = path [0];
     targetIndex = 0;

Out of range means you tried to access a index of a array that does not exist.

You debug code should have shown you that the path does not have any length. (length = 0)
Trying to access index 0 does not work in that case.
Try to figure out why the path did not have any points and try to prevent the function to start in case the path is not valid.