# For Loop Array Problem

Good morrow, good morrow,

I have this piece of code looping through the star scores and then producing either a gold, silver or bronze for the amount of stars collected for each level. The code seems only to work on the last item in the array. So all of the levels have the value of the 54th array item. In this case every level echo’s out a gold trophy. This output is controlled by another bit of code which prints an array of the level, score and trophy.

``````trophy = new String[54];
starScores = new float[54];

for(var n : int = 0; n<starScores.length; n++){

starScores[n] = PlayerPrefs.GetFloat("s"+(n+1));

for(var j : int = 0; j<54; j++){

if(starScores[n] >= 5 && starScores[n] < 10){
trophy[j] = "gold";
} if(starScores[n] == 3 && starScores[n] < 5){
trophy[j] = "silver";
} if(starScores[n] == 0 && starScores[n] < 3){
trophy[j] = "bronze";
}

}
}
totalStarScore = starScores[0] + starScores[1] + starScores[2] + starScores[3] + starScores[4] + starScores[5] + starScores[6] + starScores[7] + starScores[8] + starScores[9] + starScores[10] + starScores[11] + starScores[12] + starScores[13] + starScores[14] + starScores[15] + starScores[16] + starScores[17];
array4 = [trophy[0],trophy[1],trophy[2],trophy[3],
trophy[4],trophy[5],trophy[6],trophy[7],
trophy[8],trophy[9],trophy[10],trophy[11],
trophy[12],trophy[13],trophy[14],trophy[15],
trophy[16],trophy[17],trophy[18],trophy[19],
trophy[20],trophy[21],trophy[22],trophy[23],
trophy[24],trophy[25],trophy[26],trophy[27],
trophy[28],trophy[29],trophy[30],trophy[31],
trophy[32],trophy[33],trophy[34],trophy[35],
trophy[36],trophy[37],trophy[38],trophy[39],
trophy[40],trophy[41],trophy[42],trophy[43],
trophy[44],trophy[45],trophy[46],trophy[47],
trophy[48],trophy[49],trophy[50],trophy[51],
trophy[52],trophy[53]];
``````

Any idea of the problem within this code i’m guessing its got something to do with the two for loops! Any help would be much appreciated. Thank You.

trophy = new String[54];
starScores = new float[54];

``````    for(var n : int = 0; n<starScores.length; n++){

starScores[n] = PlayerPrefs.GetFloat("s"+(n+1));

if(starScores[n] >= 5 && starScores[n] < 10){
trophy[n] = "gold";
} if(starScores[n] >= 3 && starScores[n] < 5){
trophy[n] = "silver";
} if(starScores[n] >= 0 && starScores[n] < 3){
trophy[n] = "bronze";
}

}
totalStarScore = starScores[0] + starScores[1] + starScores[2] + starScores[3] + starScores[4] + starScores[5] + starScores[6] + starScores[7] + starScores[8] + starScores[9] + starScores[10] + starScores[11] + starScores[12] + starScores[13] + starScores[14] + starScores[15] + starScores[16] + starScores[17];
array4 = [trophy[0],trophy[1],trophy[2],trophy[3],
trophy[4],trophy[5],trophy[6],trophy[7],
trophy[8],trophy[9],trophy[10],trophy[11],
trophy[12],trophy[13],trophy[14],trophy[15],
trophy[16],trophy[17],trophy[18],trophy[19],
trophy[20],trophy[21],trophy[22],trophy[23],
trophy[24],trophy[25],trophy[26],trophy[27],
trophy[28],trophy[29],trophy[30],trophy[31],
trophy[32],trophy[33],trophy[34],trophy[35],
trophy[36],trophy[37],trophy[38],trophy[39],
trophy[40],trophy[41],trophy[42],trophy[43],
trophy[44],trophy[45],trophy[46],trophy[47],
trophy[48],trophy[49],trophy[50],trophy[51],
trophy[52],trophy[53]];
``````

I think this should work for you. No need of second for loop. Check rectified if conditions for silver and bronze also. Try and let me know if working fine… thanks.