Confused about Array - [i - 1].....

I’m having a hard time understanding the “gearSpeeds[i-1]” part of the for loop below. Isn’t [i-1] equal to current array index - 1?

var topSpeed : float = 160;
var numberOfGears : int = 5;
private var engineForceValues : float[];
private var gearSpeeds : float[];

function SetupGears()
{
	engineForceValues = new float[numberOfGears];
	gearSpeeds = new float[numberOfGears];
	
	var tempTopSpeed : float = topSpeed;
		
	for(var i = 0; i < numberOfGears; i++)
	{
		if(i > 0){
			gearSpeeds[i] = tempTopSpeed / 4 + gearSpeeds[i-1];
Debug.Log("gearSpeeds:  " + gearSpeeds[i]);
}

When I debug the "gearSpeeds = tempTopSpeed / 4 + gearSpeeds[i-1];" with "Debug.Log(“gearSpeeds: " + gearSpeeds*);” it gives me the following results:*
gearSpeeds: 70
gearSpeeds: 92.5
gearSpeeds: 109.375
gearSpeeds: 122.0312
But if I remove “+ gearSpeeds[i-1]” and debug again, I get:
gearSpeeds: 30
gearSpeeds: 22.5
gearSpeeds: 16.875
gearSpeeds: 12.65625
What I can’t figure out is how “gearSpeeds[i-1]” equals 40 in the first index (30+40=70)???
I’m finally starting to understand Arrays and looping through them with for loops, but the [i-1] results are making my brain smoke…lol

Are there some lines missing?

Also 160/4 is 40. So I have no idea how you managed to get those 2 print outs.

40,80,120,160 is that supposed to be the result?

Does Javascript initialize arrays to zero?
C# does. Try setting
gearSpeeds[0] = 0;

Nope there’s no lines missing, that code comes straight from the Unity Car tutorial.
And yes Javascript initialize arrays to zero.

That’s why I’m confused…160/4 = 40, so how does 160/4 + gearSpeeds[i-1] = 70??? Why is gearSpeeds[i-1] = 30???

What is gearspeeds[0]?

The for loop in your code snippet doesn’t close; are there other lines in that for loop that could alter your data?

Log tempTopSpeed and gearspeeds[0] before the for loop and I’m sure you’ll find where the problem is (unless it’s further down the for loop).

Actually yes there’s an else statement which is:

else
gearSpeads[i] = tempTopSpeed / 4;

But whether I use the else statement or not gives me the same results when logging the if statement.
Oh and gearSpeads[0] returns 0 in the debugger.

Don’t post the same thing on the forum and UnityAnswers!

:evil:

TempTopSpeed is being changed in your for loop.

gs[0] = 40
gs[1] = 40 + 30
gs[2] = 70 + 22.5
gs[3] = 92.5 + 17.875

Somewhere in the for loop, tempTopSpeed is being multiplied by 0.75.

Your “else” that you mentioned is how gearSpeeds[0] == 40.

aaaahhh this is starting to make sense now, thx for your time and help Vicenti. I’m gonna figure it out with this info :wink:

Sorry about that, I’ll make sure I don’t do it again in the future :sweat_smile:

Vicenti, here’s the full function below…

function SetupGears()
{
	engineForceValues = new float[numberOfGears];
	gearSpeeds = new float[numberOfGears];
	
	var tempTopSpeed : float = topSpeed;
		
	for(var i = 0; i < numberOfGears; i++)
	{
		if(i > 0){
			gearSpeeds[i] = tempTopSpeed / 4 + gearSpeeds[i-1];
			//Debug.Log("gearSpeeds if:  " + gearSpeeds[i]);
		}
		else{
			gearSpeeds[i] = tempTopSpeed / 4;
			//Debug.Log("gearSpeeds else:  " + gearSpeeds[i]);
		}
		
		tempTopSpeed -= tempTopSpeed / 4;
	}
}

You were right, tempTopSpeed is being changed at the end of the loop:

tempTopSpeed -= tempTopSpeed / 4;

I should have seen this earlier…it’s hard being a noob to coding, thank god for ppl like you willing to help :smile:
So if I understand correctly, then the value of tempTopSpeed is being multiplied by 0.75 after the if/else statement gets executed?

Oh really :slight_smile:

tempTopSpeed -= tempTopSpeed / 4;

Dividing it by 4 then subtracting it from itself is the same as x.75

xD

Glad to be of help.