IndexOutOfRangeException: Array index is out of range.

Hi,i’m new. Sorry for my english
I’m trying to create a script for my enemy.In the script the enemy go along between two point(waypoint).When he sees the player ho goes to the player.If the distance between the enemy and the player is <5 the enemy shots a gun(prefab).if the distance from the waipont is >30 the enemy has to come back.if the player is hidden the enemy doesn’t see him.This function is an implementation of another script.
I riceve always the same error and i don’t uderstand why:

IndexOutOfRangeException: Array index is out of range.
the script is:
var waypoint:Transform;

var speed:float=20;

private var currentWaypoint:int;

var site:GameObject;

var loop:boolean=true;

var shootgun:boolean=false;

var player:Transform;

function Update () {

if(currentWaypoint5){

if(!site.safe){

velocity=Vector3.zero;

target=player.position;

 velocity=(player.position -transform.position).normalized*speed;

  velocity=moveDirection.normalized*speed;

if((player.position - waypoint[currentWaypoint].position).magnitude>30){

 target=waypoint[currentWaypoint].position;

 velocity=moveDirection.normalized*speed;

}}

else{

if((site.safe)||((player.position - waypoint[currentWaypoint].position).magnitude>30)){

target=waypoint[currentWaypoint].position;

velocity=moveDirection.normalized*speed;}}}

else{

if(distFromPlayer.magnitude<5){

if(!site.safe){

 velocity=Vector3.zero;

 target=player.position;

  velocity=(player.position -transform.position).normalized*0;

  shoot();

if((player.position - waypoint[currentWaypoint].position).magnitude>30){

 target=waypoint[currentWaypoint].position;

 velocity=moveDirection.normalized*speed;

} }

else{

if((site.safe)||((player.position - waypoint[currentWaypoint].position).magnitude>30)){

target=waypoint[currentWaypoint].position;

velocity=moveDirection.normalized*speed;}}

}

}

}else{

if(loop){currentWaypoint=0;

    }

 else{velocity=Vector3.zero;

 }

 }

rigidbody.velocity=velocity;

transform.LookAt(target);

}

//ogni unità di tempo spara un colpo

var spawnPos : Transform;

var ball : Rigidbody;

var shotForce : float = 5;

var nextFire = 2;

function shoot(){

if(Time.time >= nextFire)

{ nextFire = nextFire+5;

var pFab = Instantiate(ball, spawnPos.position, spawnPos.rotation);

pFab.velocity= transform.forward * shotForce;

Destroy(pFab.gameObject, 4);

}}
can someone help me?

u might need to declare ur array before u can use it.

i just assumed u r waypoint variable is a vector3 but u can change it to whatever u want

   private var waypoint: Vector3[]; 

or it could be that it empty make sure it has something.

gl