• 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
Question by Enzoseurre · Oct 07, 2022 at 03:23 PM · index

Index was out of range - List of Token

Hi,

I have a problem that I can't resolve. More precisely I don't understand why "i" is out of range with an upper limit with turnNumber. Below are the two linked codes that cause the problem and the error in unity with the two lines causing the error. Thank you in advance for the help.

 public void TurnConstructor()
     {
         playerNextMove.Clear();
         foreach (Token token in selectedToken)
         {
             playerNextMove.Add(int.Parse(token.text.text));
         }
         foreach (GameObject token in playerTokens)
         {
             token.SetActive(false);
         }
         selectedToken.Clear();
 
         activeTurns.Clear();
         
         int turnNumber = enemyNextMove.Count;
 
         for (int i = 0; i < turnNumber; i++)
         {
             if (i < turnNumber - 1)
             {
                 activeTurns.Add(enemyNextMove[i]);
                 isEnemyTurn.Add(true);
                 activeTurns.Add(playerNextMove[i]);
                 
 
             }
             else
             {
                 activeTurns.Add(enemyNextMove[i]);
                 //break;
             }
         }
         
         StartCoroutine(WaitBeforeStart());
 
     }


 public void PlayTurn()
     {
         if (activeTurns.Count > 0)
         {
             if (enemyTurn)
             {
                 int moveValue = activeTurns[0];
                 StartCoroutine(MoveToNext(enemy, enemyIndex, moveValue));
                 activeTurns.RemoveAt(0);
             }
             else
             {
                 int moveValue = activeTurns[0];
                 StartCoroutine(MoveToNext(player, playerIndex, moveValue));
                 activeTurns.RemoveAt(0);
             }
         }
         else
         {
             Debug.Log("all turns done");
         }
 
     }





alt text

2022-10-07-17-16-54-trappe-trappe-samplescene-wind.png (30.5 kB)
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by tormentoarmagedoom · Oct 08, 2022 at 06:43 AM

Its very hard to find why is out of index without trying in "live".. You must DEBBUG your code while running andlook at line 219 of you code and check the values of its variables until find wichone and when is out of range. Then investigate why.

Another easy solution i to place a Dubeug.Log command with all variable values right before that line (or inside the loop if its a loop) to get info about what value can be doign this....

Bye.

Comment
Enzoseurre

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

What purpose do indexers have? 1 Answer

[ANSWERED] IndexOf and LastIndexOf both returning -1 when an item is on the list. 1 Answer

How do i get array index that contains spesific string 1 Answer

Survival Shooter LineRenderer.setPosition index out of bounds 1 Answer

index error 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