Inventory "Remove" Function Help

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);*
* }*
* }*
}

Just Add PlayerPrefs.DeleteAll(); at the beginning of your SaveItems() method, so that it will save the current state of the inventory deleting the previous contents.

Other than that, removing the item from the inventory by simply removing it from the list like you do in RemoveItem is fine.

public void SaveItems() {
    PlayerPrefs.DeleteAll();
    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);*
}
}
Edit: In case you are using PlayerPrefs to store other things except inventory, then you might not want to delete all keys, but only delete the inventory keys. It’s a bit more complicated, you’ll need to check which keys exist and delete them. If this is the case, replace PlayerPrefs.DeleteAll(); with the following code:
for (int i = 0; PlayerPrefs.HasKey(“WeaponsInventory” + i); i++) {
PlayerPrefs.DeleyeKey(“WeaponsInventory” + i);
}
// repeat with “ArmorInventory” and “ItemsInventory”