Index was out of range error when trying to use specific weapons.

Hi everyone! :slight_smile: I am experiencing a strange error that throws when I try to equip specific weapons from my inventory that states 'an index was out of range. Must be a non-negative and less than the size of the collection." What confuses me about the error is that I haven’t changed any code or moved the hierarchy around in anyway so I am unsure of why the error is being thrown all of a sudden. Any helping hands you guys can lend would be very appreciated! :smile:

What is also strange is that I only get the error while trying to equip the Axe and the Glock, but the Flashlight works fine. I should clarify that these weapons are connected to separate arm meshes, they are children of the player character “Sedgewick” but can be moved to and away from the parent. When I expand the error it leads me toward my GAMEUI prefab, which holds all of the games UI elements and scripts for important systems (save, load, etc.) I do have an Inventory script attached to it as well but when I search through it nothing seems out of the ordinary or different from how I had it before when it was functioning properly. I though maybe the negative values attached to the item switcher script within my player character might be causing the problem, but nothing changes when I switch them out for a positive or nil value.
Diving into the code highlights these lines on these specific scripts. This where I am super lost :frowning: I myself, am not much of a programmer and cant make sense of what is wrong. Again I don’t see how something could have gone wrong with the code if I have not changed it, the only big change to the project that comes to mind around when i started experiencing the error was installing a preview memory profile package, but I’m unsure how that could cause this error specifically.

I’d be happy to provide anymore materials you guys need to help me crack this error, its been such a pain especially since I just cant pin point what exactly is going wrong.

Thankyou!


ItemSwitcher line 122 is not the source of your error. It’s just a bracket. So you modified the script after getting the error or you forgot to save it and saved it after you got the error.

That being said, post code using code tags in the forums. Copy and paste the error, it contains full stack trace info.

As far as the error goes, it is pretty straight forward. You are trying to access an index in a collection that doesn’t exist. Since we can’t really see your code and the error points to a line that is not the real source of the error, I can’t tell you much. You just need to make sure your collection has what you think it has in it. Don’t try to access index 4 if it only has 3 items in it. Or, for that matter, even if it has 4 items, index 4 would not be valid (remember, collections are 0 based).

So, really the error should be just as easy to solve as a null ref error.

Agreed. Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

Steps:

  • find which collection it is (critical first step!)
  • find out why it has fewer items than you expect
  • remember you might have more than one instance of this script in your scene/prefab

Thankyou both so much for your help, I looked into the code and ended up resetting it to get back to where I was, so all fixed! :smile: Thanks again!