Error: The array index is outside the permissible range

Can someone assist me in understanding the cause of this error?

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

Snippet from 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[j]>MinEngineRPM){
            AppropriateGear=j;
            break;
        }
    }
   CurrentGear=AppropriateGear;
}

Answer №1

It appears that the issue lies within this specific line of code.

EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm) / 2 * GearRatio[CurrentGear];

    FrontLeftWheel.motorTorque = (EngineTorque / GearRatio[CurrentGear]) * motorInputTouch;
    FrontRightWheel.motorTorque = (EngineTorque / GearRatio[CurrentGear]) * motorInputTouch; 

Answer №2

Take a close look at the final if condition within the ShiftGears function:

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

You have mistakenly utilized i to reference the GearRatio array while your loop is iterating based on j. Adjust this section to utilize j instead of i.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Access and Modify Elements in a Multidimensional Array

I am currently working with a multidimensional array in PHP that stores information about vehicles: Array ( [0] => Array ( [VehicleClass] => Car [IsNew] => 1 [Year] => 2016 [Make] => Cadillac ...

The Vue v-for directive encountered an unrecognized property during rendering

Trying to grasp the concept of v-for in Vue JS, especially since I am a newcomer to this framework. Since I am utilizing Django, custom delimiters are necessary. I have a script example that appends a list of objects to a data property: var app = new Vue( ...

Add a npm module without type definitions

I am currently utilizing Typescript version 2.1 and facing an issue with installing an npm package called 'reactable' that lacks typings. When attempting to import the package using import * as Reactable from 'reactable', Typescript di ...

How can I transfer a PHP variable into a JavaScript variable nested within another PHP variable

After my php/html script generates the image, I am looking to display it without needing to reload the page. I currently call a javascript function within the PHP code to change the image. However, I am unsure how to pass the new image name to this javasc ...

Is there a way to maintain the checked status of the checkboxes after a page refresh?

Is there a way to keep the checkboxes checked even after the page is refreshed in this code snippet? Any code sample or explanation on how to achieve this would be highly appreciated. The complete project code can be found here: https://github.com/Orelso/P ...

Why does serializing a JavaScript Object to JSON result in "{}"?

Hello fellow developers, I'm currently working on setting up a LocalStorage feature for my web application. One issue I've come across is that all objects in the Model abstraction layer need to be serialized. I understand that functions aren&a ...

Error: A property called 'node' is undefined and cannot be read

Currently attempting to complete the NPM installation process. I initiated with the following command: brew install node followed by curl -O https://www.npmjs.org/install.sh and then ~ % sh install.sh The resultant output is as follows: tar=/usr/bi ...

Choose an option from a list of items in a nested array by

I'm working with a nested array (3d) and I want to populate a drop-down select menu with its values using PHP and jQuery I've tried implementing this for two-level arrays like categories and sub-categories, but what if some sub-categories have f ...

Enter your password using the text input field on Internet Explorer 8

I'm currently developing a website and facing an issue with the password input field: When clicking on it in browsers like IE9, Chrome, and Mozilla, the text changes to allow entry of the password. However, in IE8, clicking on the input field leaves ...

"Troubleshooting the issue of nested jQuery Ajax requests failing to execute properly

UPDATE: I'm getting an error saying that my li tag is considered an illegal token. I'm unsure how to resolve this issue as I believed I was appending it within a ul tag I'm tasked with fetching a JSON array of public Github repositories and ...

A Nuxt plugin that integrates a separate website into the serverMiddleware

The concept Imagine having a main Nuxt website just like any other. Now, think about adding my module to your project. This module will then introduce a subdomain "admin.example.com" to your project, creating a fully functional Nuxt-based website that ope ...

Determine the difference between a CSS selector string and an XPath string

Developing a compact querying module (using js) for html is my current project, and I aim to create a versatile query(selector) function that can handle both css selectors and XPath selectors in string form. My dilemma lies in determining whether a given ...

Vertical tab design in Bootstrap does not automatically switch tabs

I'm managing two separate tab boxes that switch content when clicked or over a 5-second period. https://i.sstatic.net/Ujz9H.jpg https://i.sstatic.net/sFc1K.jpg The left box is functioning correctly, but the right box is changing the active state wit ...

Why does the HTML and CSS slider fail to load upon page refresh, yet works perfectly when the next or previous button is clicked?

There seems to be an issue with the slider images not loading on page refresh. Oddly enough, they appear only after clicking the next button. However, upon refreshing the page again, the images disappear. <div class="slide-banner"> < ...

Making an Ajax request with JSON is yielding unexpected variables that cannot be modified or removed

Attempting to make an AJAX call using a script: $.ajax({ url: pageURL, data: loadData, type: 'POST', cache: false, dataType: 'json', success: function (data) { //if the call was successful console.log(su ...

Display handpicked ionic list view

I have put together a demonstration using this (demo) to showcase the issue I am facing. Within this demo, there is a page with a list, a button located in the header that checks the attribute of an <ion-checkbox>, and a checkbox next to the list v ...

Remove data from MySQL using a JavaScript function

I have a database with a list of items that I want users to be able to delete by clicking on a link. I need this to happen without refreshing the page. Although I am not very experienced with JavaScript, I am trying my best. This is how I currently have i ...

Sending a directive as an argument to a parent directive function

edit: I made adjustments to the code based on stevuu's recommendation and included a plunkr link here Currently, my goal is to make a child directive invoke a method (resolve) through another directive all the way up to a parent directive. However, I ...

Toggle Vue transitions on and off with a boolean parameter

Is there a way to dynamically disable a transition animation in Vue based on a boolean value? Currently, the animation is enabled with the following code: <transition name="fadeUp"> <div v-if="elIsVisible"> <p>Foo Bar</p> ...

Struggling to allocate an item to a two-dimensional array in a loop situation

Currently, I am facing an issue while trying to assign an item to mapTiles[x][i] within a loop using this line of code: mapTiles[x][i] = mapCols[i]; However, the error message that I encounter is: incompatible types - found java.lang.String but expected ...