I get a weird error every frame. I know what gives the error, but I have no clue why it gives it that error. The object this script moves like it is meant to do, with the no error in the moving.
This is the one that gives the error:
waypoints[currentWP].position
This is the error:
ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
Parameter name: index
0
System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message)
System.Collections.ArrayList.get_Item (Int32 index)
UnityScript.Lang.Array.get_Item (Int32 index)
EnemyMove: The script that produces the error
var waypoints = new Array ();
var currentWP : int;
var moveSpeed : float = 1.0;
var rotSpeed : float = 25.0;
var damping = 6.0;
function Update () {
transform.position = Vector3.MoveTowards(transform.position, waypoints[currentWP].position, moveSpeed * Time.deltaTime);
var rotation = Quaternion.LookRotation(waypoints[currentWP].position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function OnTriggerEnter (other : Collider) {
for (var waypoint : Transform in waypoints){
if (other.name == waypoint.name){
currentWP = parseInt (other.name);
}
}
}
EnemyLane: The script that gives the variable to the other script
static var waypoints : Transform[];
var prefab : Transform;
static var prefab2 : Transform;
static var laneLevel : int = 1;
var monsterType1 : String[];
var monsterSize1 : int[];
function Awake () {
waypoints = new Transform[transform.childCount];
prefab2 = prefab;
var test : int;
for (var child : Transform in transform) {
test = parseInt (child.name) - 1;
waypoints[test] = child;
}
}
function SendLane (laneLevel : int, monsterType : String[], monsterSize : int[]) {
// Find the right enemy prefab in the RESOURCES folder.
// First enemy: monsterType[0] = monsterSize[0]
var count : int;
var timer : float;
while (count < monsterSize[0]) {
SpawnMonster (timer, prefab2);
count ++;
timer += 1.0;
}
}
function Start (){
SendLane (laneLevel, monsterType1, monsterSize1);
}
function SpawnMonster (waiter : int, monsterPrefab : Transform){
yield WaitForSeconds (waiter);
var monster = Instantiate (monsterPrefab, waypoints[0].position, Quaternion.identity);
var other : EnemyMove = monster.gameObject.GetComponent(EnemyMove);
var test : int = 0;
other.waypoints = waypoints;
}