Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 imp903 · Jun 25, 2015 at 12:47 AM · javascriptloopinventorycrafting

loop index error? JS

So I'm not entirely sure why this is happening, because it has been working for a few days and then I opened unity today and after cleaning up a few scripts that seemed rather unconnected, the function of crafting a weapon stopped working and returned this error:

ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index

I included a debug.log and it seems that the problem might be with my remove items after crafting function that loops through the inventory and checks if the required item is there, and if it is, it removes it. the inventory is simply an array of strings (not a bulletin array, but a normal mutable one). The debug.log is indicating that it is able to loop through and remove two of the three items, and then the game stops because this error message is returned. here's the script:

 function removeItems()
 {
     removed = true;
     var psText : Text = powerSource.GetComponentInChildren.<Text>();
     initialBagLength = inventory.itemBag.length;
     for (var i = 0; i<initialBagLength; i++)
     {
         if (inventory.itemBag[i] != null)
         {
             Debug.Log("item removed. length is now " + inventory.itemBag.length + "index is now " + i);
             if (inventory.itemBag[i] == requiredStock)
             {
                 inventory.itemBag.RemoveAt(i);
             }
             if (inventory.itemBag[i] == requiredBarrel)
             {
                 inventory.itemBag.RemoveAt(i);
             }
             if (inventory.itemBag[i] == psText.text)
             {
                 inventory.itemBag.RemoveAt(i);
             }
         }
     }
 }

pstext just refers to the text that is user inputted into a variable slot for the power core during the crafting process. I made an initial bag length variable at the beginning of the script and defined it here, thinking the shortening length might be the problem, but it didn't seem to fix it. Unless it some other loop that consistently interferes with this loop when it gets to the point of detecting the third object for deletion. It doesn't seem to matter whether or not I have more items in my inventory than just the three required and it always prints that the first two items are removed and then fails at the third, regardless of the order in which the items are removed (based on the order i picked them up in). Not sure what exactly is happening, but any help is appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by maccabbe · Jun 25, 2015 at 04:58 AM

Well let's consider your case where you have 3 objects and are trying to remove all 3.

You start with {a, b, c} and i=0 so you remove the first object, a. Now you have {b, c} and i=1 so you remove the second object, c (uh oh). Now you have {b} and i=2 and are attempting to remove the third object from a list that only contains one item, which you can't.

You have to take into account that the list changes as you add and remove items. For instance

 for (var i = 0; i<inventory.itemBag.length;)
 {
     if (inventory.itemBag[i] != null&&
         (inventory.itemBag[i] == requiredStock||inventory.itemBag[i] == requiredBarrel||inventory.itemBag[i] == psText.text)){
             inventory.itemBag.RemoveAt(i);        
             Debug.Log("item removed. length is now " + inventory.itemBag.length + "index is now " + i);
     }
     else{
         i++;
     }
 )

This code only increments i if the object is not deleted and compares i to the actual length of the list instead of the length at start. If an object is deleted from the list then the next object is already at i and incrementing i skips checking it. In addition, if you delete an object the the size of the list changes so checking the entire original list means you would be checking indicies that are no longer there.

Comment
Add comment · Show 1 · 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
avatar image imp903 · Jun 25, 2015 at 09:59 PM 0
Share

Thanks for the great response, that completely solved it!

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

21 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

Related Questions

Crafting system 2 Answers

Playing multiple sounds with multiple subtitles 1 Answer

Does anyone know of good tutorials for learning arrays and classes? 1 Answer

AI isn't looping (Javascript) 1 Answer

Audio not playing (Radio station-like) 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