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 /
This question was closed Oct 08, 2015 at 11:02 PM by ThePokedog1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ThePokedog1 · Oct 07, 2015 at 02:43 AM · c#inventoryargumentoutofrangeexception

How do I get my Inventory Loading to work again? ArgumentOutOfRangeException

My problem is that I get an ArgumentOutOfRangeException when I go to reopen my inventory after saving. The items don't show up and I get this error.

My Code

  private List<Item> inventory = new List<Item>();
         void Start () 
         {
             LoadInventory();
             StartCoroutine(Saving());
             
         }
         IEnumerator Saving()
         {
             while(true)
             {
                 SaveInventory();
                 yield return new WaitForSeconds(5);
             }
         }
         void SaveInventory () 
         {
             for (int i = 0; i < inventory.Count; i++) 
             {
                 PlayerPrefs.SetInt("Inventory " + i, inventory[i].itemID);
             }
         }
         void LoadInventory () 
         {
             for (int i = 0; i < inventory.Count; i++) 
             {
              
                 inventory[i] = PlayerPrefs.GetInt("Inventory " + i, -1) >= 0 ? database.items[PlayerPrefs.GetInt("Inventory " + i)] : new Item();
                 
             }
             
         }
 

So I keep getting an:

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Item].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) Inventory.LoadInventory () (at Assets/Scripts/Items/Inventory.cs:899) Inventory.Start () (at Assets/Scripts/Items/Inventory.cs:79)

I figure its inventory[i] = PlayerPrefs.GetInt("Inventory " + i, -1) >= 0 ? database.items[PlayerPrefs.GetInt("Inventory " + i)] : new Item(); Doesn't make since because I had it saving when I first made it. Now it just won't load the saved items. I put a print(i);before it and it returned 12.

Edit:: I added a debug back and it returns 12 not 0 like I had said before.

Comment
Add comment · Show 1
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 ThePokedog1 · Oct 08, 2015 at 02:00 AM 0
Share

Also, if I hookup a GUI to save and load in-game, it works, but if I turn it off and turn it back on again, it will give me this error.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by ThePokedog1 · Oct 08, 2015 at 11:01 PM

Not completely sure what got it to work, but after I was messing around with a different portion my game I got it to work. It was most likely that I had way to many GameObject.Find("")s. I was watching the beginning of https://www.youtube.com/watch?v=kgr6UnyKWd8 when I changed most of my databases and scripts unto a single object so that I can avoid the GameObject.Find("")s all together. Now I don't have the problem so all good!

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
avatar image
1

Answer by etopsirhc · Oct 08, 2015 at 01:13 AM

my best guess would be because you are adding items to an empty inventory by index when nothing has been added to expand the inventory to it's normal size.

when you instantiate the inventory set a capacity, i think that will let you access it how you want, if not, try inventory.Insert(index,item);

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

Follow this Question

Answers Answers and Comments

30 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

Related Questions

Inventory help 0 Answers

Retrieving values from list of Scriptable Objects 1 Answer

Problem with using an item from inventory 0 Answers

Can't access methods from class 1 Answer

Problem when acessing a list from another script? (ArgumentOutOfRangeException) 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