Array Index is out of range
I'm having trouble trying to set up some stats using a for loop. I'm getting an array index is out of range. The thing is I know for sure that I've set up all the arrays in the inspector and all the other arrays are acting normally (at least for now).
The code is kinda long so here's the affected function, it's giving me this error once for line 5 and then every update for line 6.
function SetStats()
{
for(var stats : float in health)
{
health[stats] = ((baseHealth[stats] + itemHealth[stats]) * healthBoost[stats]);
energy[stats] = ((baseEnergy[stats] + itemEnergy[stats]) * energyBoost[stats]);
shield[stats] = ((baseShield[stats] + itemShield[stats]) * shieldBoost[stats]);
attack[stats] = ((baseAttack[stats] + itemAttack[stats]) * attackBoost[stats]);
steal[stats] = ((baseSteal[stats] + itemSteal[stats]) * stealBoost[stats]);
buff[stats] = ((baseBuff[stats] + itemBuff[stats]) * buffBoost[stats]);
}
for(var stats : float in attackDamage)
{
attackDamage[stats] = ((baseAttackDamage[stats] + itemAttackDamage[stats]) * attackBoost[stats]);
penetration[stats] = ((basePenetration[stats] + itemPenetration[stats]) * penetrationBoost[stats]);
armor[stats] = ((baseArmor[stats] + itemArmor[stats]) * armorBoost[stats]);
}
for(var stats : float in ability)
{
ability[stats] = ((baseAbility[stats] + itemAbility[stats]) * abilityBoost[stats]);
}
for(var stats : float in movement)
{
movement[stats] = ((baseMovement[stats] + itemMovement[stats]) * movementBoost[stats]);
}
}
I'm unfamiliar with the language used, so to verify: for(var stats : float in health) Is that looping of each float that the 'health' array has? Or is that somehow using 'stats' as an index?
In c#, I would do: for(int i = 0; i < health.Length; i++){ health[i] = ... energy[i] = ... ... }
The way I am reading what you have would look like this in C#: foreach(var stats in health){ health[stats] = ... energy[stats] = ... ... }
Which is going to throw out-of-range errors because of: health = [1, 10, 15]; energy = [1, 10, 35];
Looping over the 'health' array and using its values as indexes would be like this:
health[1] (works, returns 10)
energy[1] (works, returns 10)
health[10] (errors)
energy[10] (errors)
You're using a float "stat" to try and access an integer value in an array? I don't believe that is possible to access index 1.1f in an array. 1.1f is obviously an example of a floating point number. Try changing your stat var to an integer ins$$anonymous$$d.
Answer by bowloflol · Dec 31, 2015 at 06:17 AM
yeah i was trying to use [stats] as an index. I got it working thanks to you two :D. Here's the updated script for posterity
function SetStats()
{
for(var statsOne : int = 0; statsOne < health.Length; statsOne++)
{
health[statsOne] = ((baseHealth[statsOne] + itemHealth[statsOne]) * healthBoost[statsOne]);
energy[statsOne] = ((baseEnergy[statsOne] + itemEnergy[statsOne]) * energyBoost[statsOne]);
shield[statsOne] = ((baseShield[statsOne] + itemShield[statsOne]) * shieldBoost[statsOne]);
attack[statsOne] = ((baseAttack[statsOne] + itemAttack[statsOne]) * attackBoost[statsOne]);
steal[statsOne] = ((baseSteal[statsOne] + itemSteal[statsOne]) * stealBoost[statsOne]);
buff[statsOne] = ((baseBuff[statsOne] + itemBuff[statsOne]) * buffBoost[statsOne]);
}
for(var statsTwo : int = 0; statsTwo < attackDamage.Length; statsTwo++)
{
attackDamage[statsTwo] = ((baseAttackDamage[statsTwo] + itemAttackDamage[statsTwo]) * attackDamage[statsTwo]);
penetration[statsTwo] = ((basePenetration[statsTwo] + itemPenetration[statsTwo]) * penetrationBoost[statsTwo]);
armor[statsTwo] = ((baseArmor[statsTwo] + itemArmor[statsTwo]) * armorBoost[statsTwo]);
}
for(var statsThree : int = 0; statsThree < ability.Length; statsThree++)
{
ability[statsThree] = ((baseAbility[statsThree] + itemAbility[statsThree]) * abilityBoost[statsThree]);
}
for(var statsFour : int = 0; statsFour < movement.Length; statsFour++)
{
movement[statsFour] = ((baseMovement[statsFour] + itemMovement[statsFour]) * movementBoost[statsFour]);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612054317im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Loops when generating random string 1 Answer
C# Yield WaitForSeconds within For Loop 1 Answer
How to hold information for the split second between running game and returning to edit mode. 0 Answers
Saving the State of Text in an Array 0 Answers
How to move object from an array position to another array position? 0 Answers