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 /
This question was closed Feb 13, 2015 at 01:31 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Abacab · Feb 13, 2015 at 11:09 AM · c#soundaudiosourcecar

Array index is out of range - Why?

I've put stars next to the line that causes it. Can anybody spot the problem?

 public int[] gearRatio = new int[0];

 void EngineSound ()
         {
                 int gears = gearRatio.Length;
                 int i;
                 for (i = 0; i < gears; i++) {
                         if (gearRatio [i] > currentSpeed) {
                                 break;
                         }
                 }
                 float gearMinValue = 0.00f;
                 float gearMaxValue = 0.00f;
                 if (i == 0) {
                         gearMinValue = 0;
                 } else {
                         gearMinValue = gearRatio [i - 1];
                 }
                 gearMaxValue = gearRatio [i];*******
                 if (started) {
                         float enginePitch = ((currentSpeed - gearMinValue) / (gearMaxValue - gearMinValue)) + 1;
                         audio.volume = 1.0f;
                         audio.pitch = enginePitch;
                 } else { 
                         audio.volume = 0f;
                 }
         }



Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Bonfire-Boy · Feb 13, 2015 at 11:23 AM

Following the for loop starting at line 7, i can be >=gears (because that's the condition for ending the loop).

But gears is set to the length of your array, so the largest value you can use to index into the array is (gears-1). So using i to index into it when i>= gears will cause that error.

It's not clear what you're trying to do here, maybe something like this though...? Note in particular that if i comes out of the loop because it breaks the < gears condition, then it will be too large to use to access elements of the array. But you should also check that the array has >0 elements in case you forget to set it up in the inspector or from another function.

 for (i = 0; i < gears; i++) 
 {
     if (gearRatio [i] > currentSpeed) 
     {
         break;
     }
 }
 float gearMinValue = 0.00f;
 float gearMaxValue = 0.00f;
 if (i == 0) 
 {
     gearMinValue = 0; // this is actually redundant but I've followed your code for clarity
 } 
 else 
 {
     gearMinValue = gearRatio [i]; // lowest gear for speed
 }
 if (gears > 0) // check that the array contains some elements before trying to access the last one
 {
     gearMaxValue = gearRatio [gears-1]; // highest possible gear    
 }

 
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 Abacab · Feb 13, 2015 at 11:32 AM 0
Share

Thanks for your answer, but what would be the solution? I can't work it out from your answer. :(

avatar image Bonfire-Boy · Feb 13, 2015 at 11:35 AM 0
Share

It's not clear exactly what you're trying to do but I've added a possible solution.

It boils down to making sure a) that the array isn't empty and b) the index you're using isn't too large for the array's length, before trying to access an element of the array

avatar image Abacab · Feb 13, 2015 at 11:39 AM 0
Share

Solved my problem. Thanks a lot! :)

avatar image
0

Answer by Leslie-Young · Feb 13, 2015 at 11:41 AM

Add at top of the EngineSound() function the following ..

 if (gearRatio.Length == 0) return;

This will cause the function to not execute until your gearRatio array is set up correctly.

You are entering the function while gearRatio is not filled out. I'm guessing you are supposed to add some values via the Inspector.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image

Related Questions

Engine Sound & Turbo 0 Answers

What is your solution? (Zombie Sounds) 1 Answer

Buggy Engine Sound (pitch) 0 Answers

Help with sound clips [C#] 0 Answers

Multiple Cars not working 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