- Home /
Question by
Klaudia · Sep 28, 2012 at 07:23 AM ·
arrayindexwaypointsout of range
IndexOutOfRangeException: Array index is out of range.
Hi, can anybody help me with this? Code is without erros, but computer car in my game doesn't follow the waypoits. Here is my code:
var waypointContainer : GameObject;
private var waypoints : Transform[];
private var currentWaypoint : int = 0;
function Start () {
rigidbody.centerOfMass += Vector3(0, -.75, .25);
GetWaypoints();
}
function Update () {
rigidbody.drag = rigidbody.velocity.magnitude / 250;
NavigateTowardsWaypoint();
EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
ShiftGears();
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * inputTorque;
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * inputTorque;
FrontLeftWheel.steerAngle = 10 * inputSteer;
FrontRightWheel.steerAngle = 10 * inputSteer;
}
function GetWaypoints () {
var potentialWaypoints : Array = waypointContainer.GetComponentsInChildren( Transform );
waypoints = new Array();
for ( var potentialWaypoint : Transform in potentialWaypoints ) {
if ( potentialWaypoint != waypointContainer.transform ) {
waypoints[ waypoints.length ] = potentialWaypoint;
}
}
}
function NavigateTowardsWaypoint () {
var RelativeWaypointPosition : Vector3 = transform.InverseTransformPoint( Vector3(
waypoints[currentWaypoint].position.x,
transform.position.y,
waypoints[currentWaypoint].position.z ) );
inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude;
if ( Mathf.Abs( inputSteer ) < 0.5 ) {
inputTorque = RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude Mathf.Abs( inputSteer );
} else{ inputTorque = 0.0; }
if ( RelativeWaypointPosition.magnitude < 20 ) {
currentWaypoint ++;
if ( currentWaypoint >= waypoints.length ) {
currentWaypoint = 0;
}
}
}
Comment
You have the function 'GetWaypoints' but it isn't actually called anywhere in the code. This function won't run by itself!
Try putting GetWaypoints(); in the update function and see what happens.
Thanks for your tip, but it didn't help. GetWaypoints is in fuction Start which I forgot to paste from my original code.