I’m attempting to creating a prototype inventory system for a MMO and got stuck trying to figure out how to delete a specific playerprefs key when the item is removed (Which should happen in the “RemoveItem” function I’ve created. Also feel free to let me know how to make any part of the script more efficient and you are free to use it if you want.
The Inventory Manager Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventoryManager : MonoBehaviour {
public List<GameObject> WeaponsInInventory;
public List<GameObject> ArmorInInventory;
public List<GameObject> ItemsInInventory;
void Start() {
LoadItems();
}
public void AddItem(GameObject item, string itemClass) {
if(itemClass == "Weapon") {
WeaponsInInventory.Add(item);
}
else if(itemClass == "Armor") {
ArmorInInventory.Add(item);
}
else if(itemClass == "Item") {
ItemsInInventory.Add(item);
}
SaveItems();
}
public void RemoveItem(GameObject item, string itemClass) {
if(itemClass == "Weapon") {
WeaponsInInventory.Remove(item);
}
else if(itemClass == "Armor") {
ArmorInInventory.Remove(item);
}
else if(itemClass == "Item") {
ItemsInInventory.Remove(item);
}
}
public void SaveItems() {
for(int i = 0; i < WeaponsInInventory.Count; i++) {
if(WeaponsInInventory *!= null)*
_ PlayerPrefs.SetString(“WeaponsInventory” + i, WeaponsInInventory*.name);_
_ }*_
* for(int i = 0; i < ArmorInInventory.Count; i++) {*
_ if(ArmorInInventory != null)
PlayerPrefs.SetString(“ArmorInventory” + i, ArmorInInventory*.name);
}*_
* for(int i = 0; i < ItemsInInventory.Count; i++) {*
_ if(ItemsInInventory != null)
PlayerPrefs.SetString(“ItemsInventory” + i, ItemsInInventory*.name);
}
}*_
* public void LoadItems() {*
* for(int i = 0; i < WeaponsInInventory.Count; i++) {*
* string it;*
* it = PlayerPrefs.GetString(“WeaponsInventory” + i);*
* GameObject temp = new GameObject();*
* temp.name = it;*
* if(it != “”)*
* WeaponsInInventory.Add(temp);*
* }*
* for(int i = 0; i < ArmorInInventory.Count; i++) {*
* string it;*
* it = PlayerPrefs.GetString(“ArmorInventory” + i);*
* GameObject temp = new GameObject();*
* temp.name = it;*
* if(it != “”)*
* ArmorInInventory.Add(temp);*
* }*
* for(int i = 0; i < ItemsInInventory.Count; i++) {*
* string it;*
* it = PlayerPrefs.GetString(“ItemsInventory” + i);*
* GameObject temp = new GameObject();*
* temp.name = it;*
* if(it != “”)*
* ItemsInInventory.Add(temp);*
* }*
* }*
}