Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DeveLOLper · Aug 13, 2013 at 10:25 AM · errorarraystuck

IndexOutOfRangeException: Array index is out of range

I followed a car tutorial put up by bigmisterb (link: http://forum.unity3d.com/threads/86703-Basic-to-Advanced-Car-or-Vehicle-Tutorial) So yeah I followed the trutorial and then applied the code given at the end to the car, but I get this error saying array index is out of range. I have no idea what that means. Specifically, it says

IndexOutOfRangeException: Array index is out of range. CarController.GetCollider (Int32 n)(at Assets/CarController.js:83)

Here is the code. Can anyone help!?

//CarController1.js

var wheels : Transform[];

var enginePower=150.0;

var power=0.0;

var brake=0.0;

var steer=0.0;

var maxSteer=25.0;

function Start(){

 rigidbody.centerOfMass=Vector3(0,-0.5,0.3);

}

function Update () {

 power=Input.GetAxis("Vertical") * enginePower * Time.deltaTime * 250.0;

 steer=Input.GetAxis("Horizontal") * maxSteer;

 brake=Input.GetKey("space") ? rigidbody.mass * 0.1: 0.0;

 

 GetCollider(0).steerAngle=steer;

 GetCollider(1).steerAngle=steer;

 

 if(brake > 0.0){

     GetCollider(0).brakeTorque=brake;

     GetCollider(1).brakeTorque=brake;

     GetCollider(2).brakeTorque=brake;

     GetCollider(3).brakeTorque=brake;

     GetCollider(2).motorTorque=0.0;

     GetCollider(3).motorTorque=0.0;

 } else {

     GetCollider(0).brakeTorque=0;

     GetCollider(1).brakeTorque=0;

     GetCollider(2).brakeTorque=0;

     GetCollider(3).brakeTorque=0;

     GetCollider(2).motorTorque=power;

     GetCollider(3).motorTorque=power;

 }

}

function GetCollider(n : int) : WheelCollider{

 return wheels[n].gameObject.GetComponent(WheelCollider);

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joyrider · Aug 13, 2013 at 10:34 AM 0
Share

Are you sure you have two wheels in your wheels array? Seems like it is either empty or only has one entry; depending on where Unity generates this error (which is difficult to say, since the line numbering is kinda screwed in your code sample ^^)

avatar image vexe · Aug 13, 2013 at 10:34 AM 0
Share

I assume 'wheels' is being initialized from the inspector? - The error that you're getting means that you're getting out of the array bounds (using an index that is equal to or higher than wheels.length or less than 0) - make sure 'wheels' has 4 or more elements in it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Linus · Aug 13, 2013 at 10:37 AM

The error means that n is higher than the number of wheels in the array. If the car has 4 wheels, n should be between 0 and 3. And make sure you have set the values in the inspector.

return wheels[n].gameObject.GetComponent(WheelCollider);

Usually the debug log window will also show what line was calling CarController.GetCollider.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joyrider · Aug 13, 2013 at 10:48 AM 2
Share

Add a check in your GetCollider function like this:

 if(n<wheels.Length)
 {
     return wheels[n].gameObject.GetComponent(WheelCollider);
 }
 else
 {
     Debug.LogError("Wheel index higher than number of wheels");
     return null;
 }
avatar image Jay Gondaliya · Feb 05, 2015 at 12:51 PM 0
Share

SO Whats the Solution ? Joyrider Sir.

avatar image Joyrider · Feb 05, 2015 at 06:01 PM 0
Share

Please be more specific $$anonymous$$ Gondaliya... a solution to what? I think adding the check mentionned in my previous comment and correctly filling the wheels array is the solution here.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

error CS0117: `Time' does not contain a definition for `deltaTime' 1 Answer

i just got unity and when i open the program i get a error saying transaction is invalid 2 Answers

Make this accept gameobjects not just lights 1 Answer

error CS0246 anybody help? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges