- Home /
Weird error every frame
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:
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;ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
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;
}
Answer by Statement · Apr 30, 2012 at 03:57 PM
"Index is less than 0 or more than or equal to the list count."
It means that currentWP
is less than 0 (-1, -30, -38 etc), or more than or equal to the list count (if waypoints
contains 5 items, currentWP
5, 6, 7, 10, 2000 are invalid indices).
To translate to english: If you have five items, 0 to 4, which is item -1? Likewise, which is item 5? It doesn't exist.
Use the debugger to step through your code to see if your array is large enough for the indices you use. If not, find out why.
But if the index is wrong, why does the object find the right position to move to? It does everything as it should, just that it produces this error, that I still can't figure out how to remove. Because the index is never out of range of the array.
Your answer
Follow this Question
Related Questions
Scripting error! 1 Answer
When i call some variables to an other second script i have a problem 1 Answer
What is the problem with this code? 1 Answer
"Expecting ), found ';'" and "';' expected. Insert a semicolon at the end" 1 Answer
How would I be able to subtract from a variable when it hits something? (OnCollisionEnter) 1 Answer