Help ... IndexOutOfRangeException: Array index is out of range

Hey , i have a script for partoling ( moving from point to point ) and i have an array that storage the points , the script :
using UnityEngine;
using System.Collections;

public class Patrol : MonoBehaviour {
public Transform[ ] PatrolPoints;
private int CurrentP;
public float MoveSpeed;
void Start () {
transform.position = PatrolPoints [0].position;
CurrentP = 0;
}

void Update () {
if(transform.position == PatrolPoints[CurrentP].position)
{
CurrentP++;
}

if (CurrentP >= PatrolPoints.Length)
{
CurrentP = 0;
}

transform.position = Vector3.MoveTowards(transform.position , PatrolPoints[CurrentP].position, MoveSpeed * Time.deltaTime);

}
}

now it says that in that line : if(transform.position == PatrolPoints[CurrentP].position) there is a probelem which i dont understand why … i know that if the array lenght is 4 and you trying to get to 5 that problem will show up but i think the script is fine and there should be no porbems !! any help this just spamming my console but the patrol system work just fine but i want to get rid of this error !

P.S sorry for my english :slight_smile: and btw im just a new learner trying to understand stuff from tutorials but i knew how program befor on C# just the basics im new in unity !)

You’re code actually works when I copy it into my project. Can you try to set it up from scratch? Maybe open a new scene, put the script on it and populate the variables in the inspector.

Be aware, that comparing a Vector3 position might not give you expected results. It is actually impossible to compare floating point numbers, as they are never truly an exact value. Might be that the == operator of Vector3 already accounts for minimal inaccuracies, but just to make sure you could compare if the distance between target and current position is below a threshold.

if (Vector3.Distance(transform.position, PatrolPoints[CurrentP].position) < 0.1f)
{
    CurrentP++;
}

It does the same in other scenes too ( in the same project ) i tried it in Level 2 ( another scene ) which the patrol system work just fine but this just spamming my Console , doesnt seem to have a problem and i tried to reopen and stuff doesnt work :frowning:

If there’s more than 1 instance of Patrol in your scene then check to make sure that the array of Transforms has been filled up in each.

You can type t:Patrol into the search field above the hierarchy to search for any component of type Patrol.

Its was the first thing to check but all was filled so its something esle but thanks for trying :slight_smile:

I dident understood what to do ? i searched for Patrol in the search box left up but it dident found anything just made everything lighter ( when it doesnt find anything ) so i dont know if thats the search box you meant

im still trying to figure this out and cant find ANY problem if anyone even have a wild guess or a clue how to fix this i will realy appriciate this because right now im kinda abandoning this porject because of that problem but ill still try to figure this out and search how to solve it !) thanks for helpers or people who tried !!)

I doubt it :slight_smile: There’s a quick way to find out. Put this in your Update method before any of your existing code

if (PatrolPoints.Length == 0)
{
    Debug.Log("I'm missing patrol points!", gameObject);
    Debug.Break();
    return;
}

It’ll suppress the error spam in the console, pause the editor, and if you see the “I’m missing patrol points!” message you can click on it in the console and it will highlight the offending GameObject in the scene hierarchy.

wow dude thanks i found the damm problem and im ashamed at myself right now because i spent few hours and i dident find THAT I HAD 2 SCRIPTS on the same EnemeyObject which one of them was empty and thanks to you it highligheted it and i fixed it !! thanks bro

i dont know if its connected but now i have this messge in level 2 which i did not had this befor :

There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.

That’s pretty self explanatory. By default Audio Listeners are attached to the main camera so if you cloned the camera at any point and didn’t remove it or your camera in your first scene has a DontDestroyOnLoad on it and your second scene has another main camera then this can happen. But yeah - the message is telling you what’s happening pretty clearly.

Ok thanks as i said im preaty new to that kind of stuff and my english isnt at her finest so ,. thanks tho