• 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
Question by Conect11 · Oct 10, 2013 at 09:28 PM · arrayplayerprefsplayerprefsx

Attempting to store an Inventory Array in PlayerPrefsX

Bear with me here, there's going to be a lot of code.

Thanks to a couple of really awesome people (and thank God!) I was finally able to wrap my head around Playerprefs last night. No wonder everyone kept pus$$anonymous$$ng me in that direction, fantastic system! =) It's nearly perfect for what I need, except when it came to saving an inventory string. No worries, Uncle Google to the rescue. I found t$$anonymous$$s thread w$$anonymous$$ch lead me here. So, still using my Playerprefs I plugged PlayerprefsX into my project, and t$$anonymous$$ngs seemed to be fine until I tried to gametest, at w$$anonymous$$ch point I was met with an exception here: "The Int32 array cannot have 0 entries when setting Inventory UnityEngine.Debug:LogError(Object) PlayerprefsX:SetValue(String, IList, ArrayType, Int32, PlayerprefsX_SetValue$callable0$262_117) (at Assets/PlayerprefsX.js:264) PlayerprefsX:SetIntArray(String, Int32[]) (at Assets/PlayerprefsX.js:239) Newsavesystem:OnSave() (at Assets/scripts/Newsavesystem.js:48) $:MoveNext() (at Assets/scripts/Newsavesystem.js:" And outofarray error here: "IndexOutOfRangeException: Array index is out of range. Inventory.Update () (at Assets/scripts/Inventory.js:10)

Now, I realize that the errors are occurring on three different scripts, (one of w$$anonymous$$ch is massive) and I'm a big boy and am going to keep trying to figure t$$anonymous$$s out w$$anonymous$$le praying for an answer. Yes, I see that the console is even kind enough to show me what lines are screwing up where, but I'm far too green to know what it all means yet. (am learning, not just knowledge mooc$$anonymous$$ng, promise!) Please note, too, that my inventory system worked fine before I attempted to save it, so any errors have come since I added PlayerprefsX. Here are the scripts, starting with the inventory. God bless the brave soul who undertakes t$$anonymous$$s.


 //inventory
 static var inventoryArray : int[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
 var inventoryText : GameObject;
 
 
 
 
 function Update () {
 
 inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n" + "Hard Tack " + "[" + inventoryArray[1] + "]" + "\n" + "Water " + "[" + inventoryArray[2] + "]" + "\n" + "Apple Brew " + "[" + inventoryArray[3] + "]" + "\n";
 
 if(Input.GetButton("Stuff"))
 
 if(inventoryArray[0] > 0) {
 
 healthPotion();
 }
 if(Input.GetButton("Sword Slash"))
 
 if(inventoryArray[1] > 0) {
 
 hardTack();
 }
 }
 //inventoryArray[0]++;
 //inventoryArray[1] ++;
 
 function healthPotion ()  {
 
 Playerhealth.curHealth += 15;
 inventoryArray[0] -=1;
 }
 
 function hardTack ()  {
 
 Playerhunger.curHunger -= 5;
 inventoryArray[1] -=1;
 }

 Playerprefs (working before P.P.X - Inventory lines added after P.P.X))
 
 function Start() {
  
 OnLoad();
  
 autoSaveEnable();
  
 }
  
  
  
  
 function autoSaveEnable() {
  
  for(var x = 1; x>0; x++) {
  
 yield WaitForSeconds(5);
  
 Debug.Log("save me");
  
 OnSave();
  
  
 }
  
  
  
 }
  
  
  
  
 // a function created to save a game
  
     function OnSave()
  
     {
  
    
  
  
      
         PlayerPrefs.SetInt("CurXp", Playerhealth.curXp);
         PlayerPrefs.SetInt("MaxXp", Playerhealth.maxXp);
         PlayerPrefs.SetInt("MaxHealth", Playerhealth.maxHealth);
         PlayerPrefs.SetInt("CurHealth", Playerhealth.maxHealth);
         PlayerPrefs.SetInt("Level", Playerhealth.level);
         PlayerPrefs.SetInt("Money", Playermoney.curMoney);
         PlayerprefsX.SetIntArray("Inventory", Inventory.inventoryArray);
  
  
  
        
  
      
  
     }
  
    
  
    
  
    
  
    
  
     // a function created to load a game
  
     function OnLoad()
  
     {
  
    
  
         Debug.Log("loaded");
  
  
  
        Playerhealth.curXp = PlayerPrefs.GetInt("CurXp");
        Playerhealth.maxXp = PlayerPrefs.GetInt("MaxXp");
        Playerhealth.maxHealth = PlayerPrefs.GetInt("MaxHealth");
        Playerhealth.curHealth = PlayerPrefs.GetInt("CurHealth");
        Playerhealth.level = PlayerPrefs.GetInt("Level");
        Playermoney.curMoney = PlayerPrefs.GetInt("Money");
        Inventory.inventoryArray = PlayerprefsX.GetIntArray("Inventory");
  
        
  
     }



Comment

People who like this

0 Show 2
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 Eric5h5 · Oct 10, 2013 at 09:33 PM 0
Share

It would really be a lot better if you just linked to ArrayPrefs, rather than copypasting all the code.

avatar image Conect11 · Oct 10, 2013 at 09:37 PM 0
Share

fair enough. Was going to do that initially, but figure someone would be ticked that I didn't place the code in here.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Conect11 · Oct 11, 2013 at 06:07 PM

The answer was so simple it was staring me in the face the whole time. A quick note: t$$anonymous$$s is a "newb friendly" fix, not necessarily the right fix. In other words "fake it till you make it." I still hope to learn how to save arrays via PlayerPrefs, but t$$anonymous$$s will definitely suffice. So what do you do? Take each item, make it a static variable, and load / save via PlayerPrefs. Make sure to adjust any scripts that used that item to call the variable, rather than the array.

alt text

Comment

People who like this

0 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

16 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

Related Questions

assigning arrays with for in 1 Answer

For In Loop Fills all values in arrays problem 2 Answers

Saving Array Objects in Android. 0 Answers

PlayerPrefsX plugin error when build for windows store 3 Answers

Why is this giving me an error? (ToBuiltin problems) 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