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 /
avatar image
0
Question by dBlue · May 14, 2017 at 06:44 PM · arrayienumeratorarray-out-of-range-except

Index out of range inside IEnumerator

This is my first time using IEnumerator, and my project has been working well so far in terms of the timing of the numerical changes I want. But I've been having an error that I'm having difficulty debugging and can't find a lot of relevant questions on, and I'm not sure if the IEnumerator is why.

Basically, I created a variable called "activeCharacterState" that is either 0,1, or 2 depending on what buttons are pressed by the player. I also have several arrays that I want to use "activeCharacterState" to navigate through, by stating something like "characterShieldValue[activeCharacterState]". It seems like in most cases this works correctly, giving me first, second, or third element of the array when activeCharacterState is 0,1,or 2. However, on the bolded lines when I use "characterShieldValue[activeCharacterState]" or "characterHealth[activeCharacterState]", I get the error "Array index is out of range." I assume this means that "characterShieldValue[activeCharacterState]" is trying to reach an array element outside of the array's size, however, I see now way activeCharacterState could have gotten outside of 0,1, or 2 all of which are in the range of the array.

Here's my full code, lines of giving the error are bolded. My arrays are in italics and I change "activeCharacterState" in the "ChangeCharacterState" function above the Update function. I can elaborate if anybody needs me to. Thanks!

 private IEnumerator coroutine;
 private IEnumerator coroutineTimer;

 public Text enemyHealthText;
 public Text playerHealthText;
 public Text shieldHealthText;
 public Text shieldTotalText;
 public Text timer;

 public double enemyHealthValue;
 //public int playerHealthValue;
 //public double shieldHealthValue;
 public int shieldTotalValue;
 public int timerValue;

 public int activeCharacterState;

 public int[] characterHealth;
 public int[] characterHealthTotal;
 public int[] characterHealingRate;
 public int[] characterShieldTotal;
 public float[] characterShieldValue;
 public int[] characterShieldTotalValue;
 public double[] characterAttackRate;
 public int[] characterSpecial;


 void Start () {
     coroutine = changeHealthByTime();
     coroutineTimer = changeTimer();
     StartCoroutine(coroutine);
     StartCoroutine(coroutineTimer);

     *characterHealth = new int[3] { 100, 150, 100 };
     characterHealthTotal = new int[3] { 100, 150, 100 };
     characterHealingRate = new int[3] { 1, 1, 1 };
     characterShieldTotal = new int[3] { 2, 2, 4 };
     characterShieldValue = new float[3] { 20, 20, 20 };
     characterShieldTotalValue = new int[3] { 20, 20, 20 };
     characterAttackRate = new double[3] { 0.6, 0.3, 0.3 };
     characterSpecial = new int[3] { 1, 2, 3 };*
 }

 private IEnumerator changeTimer()
 {
     while(true)
     {
         yield return new WaitForSeconds(1f);
         {
             if (timerValue > 0)
             timerValue--;
             else
             {
                 timerValue = 10;
                 if (characterShieldValue[activeCharacterState] > 0)
                 {
                     characterShieldValue[activeCharacterState] = 0;
                 }
                 else
                 {
                     if (characterHealth[activeCharacterState] > 80)
                     {
                         characterHealth[activeCharacterState] -= 80;
                     }
                     else
                     {
                         characterHealth[activeCharacterState] = 0;
                     }
                 }
             }
         }
         yield return null;
     }
 }

 private IEnumerator changeHealthByTime()
 {
     while (true)
     {
         if (Input.GetKey("up"))
         {
             if (characterHealth[activeCharacterState] < characterHealthTotal[activeCharacterState] && characterShieldValue[activeCharacterState] <= 0)
             {
                 characterShieldValue[activeCharacterState] = 0;
                 yield return new WaitForSeconds(.0005f);
                 {
                     characterHealth[activeCharacterState]++;
                 }
             }
             if (characterHealth[activeCharacterState] < characterHealthTotal[activeCharacterState] && characterShieldValue[activeCharacterState] > 0)
             {
                 yield return new WaitForSeconds(.0005f);
                 {
                     characterHealth[activeCharacterState]++;
                 }
                 yield return new WaitForSeconds(.01f);
                 {
                     characterShieldValue[activeCharacterState] -= 0.5f;
                 }
             }
             if (characterHealth[activeCharacterState] == characterHealthTotal[activeCharacterState] && characterShieldValue[activeCharacterState] > 0)
             {
                 yield return new WaitForSeconds(.01f);
                 {
                     characterShieldValue[activeCharacterState] -= 0.5f;
                 }
             }
         }
         else
         {
             **if (characterHealth[activeCharacterState] > 0 && characterShieldValue[activeCharacterState] <= 0)
             {
                 characterShieldValue[activeCharacterState] = 0;**
                 yield return new WaitForSeconds(.01f);
                 {
                     characterHealth[activeCharacterState]--;
                 }
                 if (enemyHealthValue > 0 && Input.GetKey("down"))
                 {
                     yield return new WaitForSeconds(.01f);
                     {
                         enemyHealthValue -= 0.3;
                     }
                 }
             }
             **else if (characterHealth[activeCharacterState] > 0 && characterShieldValue[activeCharacterState] > 0)**
             {
                 yield return new WaitForSeconds(.01f);
                 {
                     characterShieldValue[activeCharacterState] -= 0.5f;
                 }
                 if (enemyHealthValue > 0 && Input.GetKey("down"))
                 {
                     yield return new WaitForSeconds(.01f);
                     {
                         enemyHealthValue -= 0.3;
                     }
                 }
             }
             else if (characterHealth[activeCharacterState] <= 0)
             {
                 characterHealth[activeCharacterState] = 0;
             }
         }
         yield return null;
     }
 }

 void UpdateCharactersHealth()
 {
     enemyHealthText.text = enemyHealthValue.ToString("000");
     playerHealthText.text = characterHealth[activeCharacterState].ToString();
     shieldHealthText.text = characterShieldValue[activeCharacterState].ToString("00");
     shieldTotalText.text = shieldTotalValue.ToString();
     timer.text = timerValue.ToString();
 }

 public void ChangePlayerHealth(int boostHealthValue)
 {
     if (Input.GetKey("space") && characterHealth[activeCharacterState] < characterHealthTotal[activeCharacterState])
     {
         characterHealth[activeCharacterState] += boostHealthValue;
     }
 }

 public void CreateShield (int shieldAmount)
 {
     if (Input.GetKeyDown("left") && characterHealth[activeCharacterState] > 20 && shieldTotalValue > 0)
     {
         characterShieldValue[activeCharacterState] += shieldAmount;
         characterHealth[activeCharacterState] -= 20;
         shieldTotalValue--;
     }
 }

 void ChangeCharacterState()
 {
     if (Input.GetKey(KeyCode.Alpha1))
     {
         activeCharacterState = 0;
     }
     if (Input.GetKey(KeyCode.Alpha2))
     {
         activeCharacterState = 1;
     }
     if (Input.GetKey(KeyCode.Alpha3))
     {
         activeCharacterState = 2;
     }
 }

 void Update()
 {
     UpdateCharactersHealth();
     ChangePlayerHealth(1);
     CreateShield(20);
     ChangeCharacterState();
     print(activeCharacterState);
 }


Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

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

Audio loop half working 0 Answers

Resizable Array of Classes? 2 Answers

Array index out of range... but it is in range? 1 Answer

whats wrong with this script 2 Answers

problem with array index out of range 1 Answer


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