Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bowloflol · Dec 30, 2015 at 01:33 AM · arrayfloatloopsstats

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]);
     }
 }

Comment
Add comment · Show 2
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 A_Li_N · Dec 30, 2015 at 06:04 AM 1
Share

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)

avatar image savlon · Dec 30, 2015 at 10:48 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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]);
     }
 }
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

38 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 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

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


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