- Home /
Engine Sound & Turbo
Hello!
Recently I decided to add turbo power-ups to my car game, but now I have a problem with the engine sound. Currently it works like this:
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 && PlayerPrefs.GetInt ("AudioIsMuted") == 0) {
float enginePitch = ((currentSpeed - gearMinValue) / (gearMaxValue - gearMinValue)) + 1;
GetComponent<AudioSource> ().mute = false;
GetComponent<AudioSource> ().pitch = enginePitch;
} else {
GetComponent<AudioSource> ().mute = true;
}
}
When the player uses a turbo power-up the top speed momentarily gets higher and the torque gets higher as well.
The problem is that whenever a turbo is used, I get the error message:
IndexOutOfRangeException: Array index is out of range. CarTouchControls.EngineSound () (at Assets/Scripts/Car/CarTouchControls.cs:264) CarTouchControls.Update () (at Assets/Scripts/Car/CarTouchControls.cs:203)
CarTouchControls.cs:264 = gearMaxValue = gearRatio [i];
CarTouchControls.cs:203 = EngineSound ();
Thanks in advance!
that always happens when u use an Array without defining its length. you have to set it by typing: arrayname = numberofslots;
Your answer
Follow this Question
Related Questions
Array index is out of range - Why? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Buggy Engine Sound (pitch) 0 Answers
simple drift car movement 0 Answers