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 pushing 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 this thread which lead me here. So, still using my Playerprefs I plugged PlayerprefsX into my project, and things seemed to be fine until I tried to gametest, at which point I was met with an exception here: “The Int32 array cannot have 0 entries when setting Inventory
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 which is massive) and I’m a big boy and am going to keep trying to figure this out while 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 mooching, 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 this.

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] + "]" + "

" + "Hard Tack " + “[” + inventoryArray1 + “]” + "
" + "Water " + “[” + inventoryArray2 + “]” + "
" + "Apple Brew " + “[” + inventoryArray[3] + “]” + "


if(inventoryArray[0] > 0) {

if(Input.GetButton("Sword Slash"))

if(inventoryArray[1] > 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() {
function autoSaveEnable() {
 for(var x = 1; x>0; x++) {
yield WaitForSeconds(5);
Debug.Log("save me");
// 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()
       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");

The answer was so simple it was staring me in the face the whole time. A quick note: this 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 this 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