0

I did not understand what is the reason of this bug, could anyone help me?

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

My code:

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;
}
6
  • 1
    Wait, is that unityscript? Commented Feb 2, 2015 at 20:13
  • This line if(FrontLeftWhell.rpm * GearRatio[i]>MinEngineRPM){ near the end, shouldn't it be [j] instead of [i]? Commented Feb 2, 2015 at 20:14
  • 1
    yes,i tried many method but i could find solutions:( Commented Feb 2, 2015 at 20:14
  • 1
    Your error says that the exception is thrown at Assets/Scripts/PlayerCar.js on line 73. What's on this line? Commented Feb 2, 2015 at 20:23
  • 1
    (71-)EngineRPM =( FrontLeftWhell.rpm + FrontRightWhell.rpm)/2 * GearRatio[CurrentGear]; (72-) FrontLeftWhell.motorTorque = (EngineTorque /GearRatio[CurrentGear]) * motorInputTouch; (73-) FrontRightWhell.motorTorque = (EngineTorque /GearRatio[CurrentGear]) * motorInputTouch; Commented Feb 2, 2015 at 20:25

2 Answers 2

1

i think , problem is just this line

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

    FrontLeftWhell.motorTorque = (EngineTorque /GearRatio[CurrentGear]) * motorInputTouch;
    FrontRightWhell.motorTorque = (EngineTorque /GearRatio[CurrentGear]) *  motorInputTouch; 
Sign up to request clarification or add additional context in comments.

Comments

0

Look at your last if statement in the ShiftGears function:

if(FrontLeftWhell.rpm * GearRatio[i]>MinEngineRPM){

Your loop is using j as the current index, but you are using i to access the GearRatio array. Use j here instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.