Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Benifir · Dec 19, 2013 at 08:41 AM · c#inventory

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[i] != null)
                 PlayerPrefs.SetString("WeaponsInventory" + i, WeaponsInInventory[i].name);
         }
 
         for(int i = 0; i < ArmorInInventory.Count; i++) {
             if(ArmorInInventory[i] != null)
                 PlayerPrefs.SetString("ArmorInventory" + i, ArmorInInventory[i].name);
         }
 
         for(int i = 0; i < ItemsInInventory.Count; i++) {
             if(ItemsInInventory[i] != null)
                 PlayerPrefs.SetString("ItemsInventory" + i, ItemsInInventory[i].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);
         }
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tomer-Barkan · Dec 19, 2013 at 08:51 AM

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[i] != null)
             PlayerPrefs.SetString("WeaponsInventory" + i, WeaponsInInventory[i].name);
     }
  
     for(int i = 0; i < ArmorInInventory.Count; i++) {
         if(ArmorInInventory[i] != null)
             PlayerPrefs.SetString("ArmorInventory" + i, ArmorInInventory[i].name);
     }
  
     for(int i = 0; i < ItemsInInventory.Count; i++) {
         if(ItemsInInventory[i] != null)
             PlayerPrefs.SetString("ItemsInventory" + i, ItemsInInventory[i].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"
Comment
Add comment · Show 2 · 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
avatar image Benifir · Dec 19, 2013 at 08:57 AM 0
Share

I thought about doing it both of those ways but they each have their drawbacks. By using PlayerPrefs.DeleteAll();, everything will be deleted (obviously :p) which includes all the players info such as name, location, cash, friends list..ect. I figured by leaving it as it is, if you delete something from the inventory then restart the game the item will reappear from the inventory because it was never deleted from playerprefs which it repopulates the inventory from.

avatar image Benifir · Dec 19, 2013 at 09:09 AM 0
Share

Great answer! Got the internals working perfectly, thanks mate :)

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.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Adding Item to an Inventory. 1 Answer

C# syntax seems fine, why wont yeild work? (Unsolved) 1 Answer

unity3d simple inventory 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