IndexOutOfRangeException

IndexOutOfRangeException

我不明白此错误的原因是什么,有人可以帮助我吗?

IndexOutOfRangeException: Array index is out of range.(at Assets/Scripts/PlayerCar.js:73)CompareApproximately (det, 1.0F, .005f)UnityEditor.DockArea:OnGUI()

我的代码:

var GearRatio : float [ ];
var CurrentGear :int= 1;
var EngineTorque:float=230.0;
var MaxEngineRPM:float=3000;
var MinEngineRPM:float=1000;
private var EngineRPM: float = 0.0;

function Start () {
        rigidbody.centerOfMass += Vector3(0, -1f, 0.25f);
}
function Update () {
        EngineRPM =( FrontLeftWhell.rpm + FrontRightWhell.rpm)/2 * GearRatio[CurrentGear];
        ShiftGears();

        FrontLeftWhell.motorTorque = (EngineTorque /GearRatio[CurrentGear]) * motorInputTouch;
        FrontRightWhell.motorTorque = (EngineTorque /GearRatio[CurrentGear]) *  motorInputTouch;
}

function ShiftGears(){

        if(EngineRPM>=MaxEngineRPM){
        var AppropriateGear: int =CurrentGear;
        for (var i=0;i<GearRatio.length;i++){
                if(FrontLeftWhell.rpm * GearRatio[i]>MaxEngineRPM){
                    AppropriateGear=i;
                    break;
                }
            }
        CurrentGear=AppropriateGear;
}

if(EngineRPM <=MinEngineRPM){
    AppropriateGear=CurrentGear;
    for (var j=0;j<GearRatio.length;j++){
        if(FrontLeftWhell.rpm * GearRatio[i]>MinEngineRPM){
            AppropriateGear=j;
            break;
        }
    }
   CurrentGear=AppropriateGear;
}

最佳答案

我认为,问题就是这条线

EngineRPM =(FrontLeftWhell.rpm + FrontRightWhell.rpm)/ 2 * GearRatio [CurrentGear];

    FrontLeftWhell.motorTorque = (EngineTorque /GearRatio[CurrentGear]) * motorInputTouch;
    FrontRightWhell.motorTorque = (EngineTorque /GearRatio[CurrentGear]) *  motorInputTouch;

关于javascript - IndexOutOfRangeException:数组索引超出范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28285900/

10-12 20:30