IndexOutOfRangeException: Array index is out of range. (606012)

Hello!

Im making a game and im new at this.. im getting error always when cube moves to 1 point.

error:
IndexOutOfRangeException: Array index is out of range.
Enemy.Update () (at Assets/Scripts/Enemy.cs:27)

and its says that problem is here:

transform.position = Vector3.MoveTowards(transform.position,EnemyP[currentPoint].position, Movement * Time.deltaTime);
using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {
    public Transform[] EnemyP;
    public float Movement;
    private int currentPoint;

    // Use this for initialization
    void Start () {
        transform.position = EnemyP [0].position;
        currentPoint = 0;
    }
   
    // Update is called once per frame
    void Update () {
        if(currentPoint >= EnemyP.Length)
        {
            currentPoint = 0;
        }
       
        if (transform.position == EnemyP[currentPoint].position)
        {
            currentPoint++;
        }
       
        transform.position = Vector3.MoveTowards(transform.position,EnemyP[currentPoint].position, Movement * Time.deltaTime);                                    
    }
}

Thanks for any help i will get !

you should check currentPoint >= EnemyP.Length (Line 17-19) after you add currentPoint++ in line 24

f.e. array.length = 5 .
current point is 4. than you check is >= array length is false so next step line 24 , 4 +1 so currentPoint index is 5.

line 27 you want tp get EnemyP[5] but as your length is 5 your max index is 4. so outoufbounds :slight_smile:

its possible that you done for me ? becuase still dont know where did u get array.length = 5 and array length is false so next step line 24 , 4 +1 so currentPoint index is 5. i mean if you change for me then maybe i will understand and sry that i dont get it im just new :stuck_out_tongue:

that number was just f.e → for example :smile:

public class Enemy : MonoBehaviour {
    public Transform[] EnemyP;
    public float Movement;
    private int currentPoint;
    // Use this for initialization
    void Start () {
        transform.position = EnemyP [0].position;
        currentPoint = 0;
    }
  
    // Update is called once per frame
    void Update () {      
        if (transform.position == EnemyP[currentPoint].position)
        {
            currentPoint++;
        }

        if(currentPoint >= EnemyP.Length)
        {
            currentPoint = 0;
        }
      
        transform.position = Vector3.MoveTowards(transform.position,EnemyP[currentPoint].position, Movement * Time.deltaTime);                                   
    }
}

maybe this should work.

problem was that you call your array with EnemyP[currentPoint].position but wenn the way you had set up your code.

First checking if index is higher or equal array lentgh

if(currentPoint >= EnemyP.Length)
        {
            currentPoint = 0;
        }

and than if positions match adding up your currentPoint

if (transform.position == EnemyP[currentPoint].position)
        {
            currentPoint++;
        }

code result in that your index at

transform.position = Vector3.MoveTowards(transform.position,EnemyP[currentPoint].position, Movement * Time.deltaTime);

→ EnemyP[currentPoint].position is to high and than you get index out of bounds

oh now i get it :slight_smile: thanks man its working!