- Home /
Set Playerpref("String" + Int)??
I'm working on loading and saving my inventory system. I could just manualy type in every single slot number I thought I would have it just cycle through the array of slots like so:
void SaveInv() {
int SlotNumTemp = 0;
while (GetComponent<InventoryBox>().Slots[SlotNumTemp] != null)
{
PlayerPrefs.SetInt("Invslot" + SlotNumTemp, InvSlots[SlotNumTemp].ItemValue);
}
}
In theory it works, but upon pressing the save button to trigger this event unity's editor just crashes...
Seems odd and I'm not sure what's wrong, any ideas?
Answer by SlowCircuit · Jun 16, 2018 at 04:12 AM
You never add to SlotNumTemp so the while loop is just evaluating the same array element over and over forever causing Unity to freeze. Just add SlotNumTemp++ into that loop.
However, it would be better to use a file saving system, not player prefs. Then you can save any data that can be serialized. You can just make the inventory a list.
Wow I feel dumb for forgetting that, yeah that fixed it. I should look into learning serialization but for some reason it is difficult for me to wrap my head around. Thank you!