Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Cjoshua2 · Dec 18, 2018 at 09:01 PM · savinginventory system

Save and load inventory

I really dont know where to start, i really dont have any idea on how to save my inventory, I would really appreciate if someone just give me a hint or a bit knowledge on saving this inventory.

 public class Inventory : MonoBehaviour {
 
     public List<GameObject> Slots = new List<GameObject>();
     public List<Item> Items = new List<Item>();
     public GameObject slots;
     ItemsDatabase database;
   
     public GameObject tooltip;
     public GameObject draggedItemGameObject;
     public Item draggedItem;
     public int indexOfDraggedItem;
     
     public bool draggingItem = false;
     
   
 
     void Update()
     {
        if(draggingItem)
         {
             Vector3 posi = (Input.mousePosition - GameObject.FindGameObjectWithTag("Canvas").GetComponent<RectTransform>().localPosition);
             draggedItemGameObject.GetComponent<RectTransform>().localPosition = posi;
             
         } 
     }
 
     public void showTooltip(Vector3 toolPosition, Item item)
     {
        tooltip.SetActive(true);
         tooltip.transform.GetChild(0).GetComponent<Text>().text = item.ItemName;
        
         tooltip.transform.GetChild(2).GetComponent<Text>().text = item.ItemDesc;
     }
     public void closeTooltip()
     {
         tooltip.SetActive(false);
     }
 
     public void showDraggedItem(Item item, int slotNumber)
     {
 
             
             indexOfDraggedItem = slotNumber;
             closeTooltip();
             draggedItemGameObject.SetActive(true);
             draggedItem = item;
             draggingItem = true;
             draggedItemGameObject.GetComponent<Image>().sprite = item.ItemIcon;
 
             
         
     }
 
     public void closeDraggedItem()
     {
         draggingItem = false;
         draggedItemGameObject.SetActive(false);
     }
 
     int x = -60;
     int y = 101;
 
     void Start () {
      
         int SlotAmount = 0;
         database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemsDatabase>();
 
         for (int i = 1; i < 5; i++)
         {
             for(int k = 1; k < 4; k++)
             {
                 GameObject slot = (GameObject)Instantiate(slots);
                 slot.GetComponent<SlotScript>().slotNumber = SlotAmount;
                 Slots.Add(slot);
                 Items.Add(new Item());
                 slot.transform.SetParent(this.gameObject.transform);
                 slot.name = "Slot" + i + "." + k;
                 slot.GetComponent<RectTransform>().localPosition = new Vector3(x, y, 0);
                 x = x + 60;
                 if(k == 3)
                 {
                     x = -60;
                     y = y - 50;
                 }
                 SlotAmount++;
             }
         }
 
         //additems to the slot, can be stacked(consumable)
         addItem(4);
         addItem(4);
         addItem(10);
 
 
         //addItem(11);
 
 
 
 
     }
 
     //stacking items in slot
     public void checkIfItemExist(int itemID, Item item) 
     {
         for (int i = 0; i < Items.Count; i++)
         {
             if (Items[i].ItemID == itemID)
             {
                 Items[i].ItemValue = Items[i].ItemValue + item.ItemValue;
                 
                 break;
             }
             else if (i == Items.Count - 1)
             {
                 addItemAtEmptySlot(item);
                 
             }
         }
     }
 
     public void addExistingItem(Item item)
     {
        
         if (item.itemType == Item.ItemType.Consumable)
         {
             checkIfItemExist(item.ItemID, item);
         }
 
         else
         {
             addItemAtEmptySlot(item);
         }
 
       
 
     }
 
 
 
     void addItem(int id)
     {
         
         for (int i = 0; i < database.items.Count; i++)
         {
             
             if (database.items[i].ItemID == id)
             {
 
                 //another if for "MATERIALS"
                 Item item = database.items[i];
                 if (database.items[i].itemType == Item.ItemType.Consumable)
                 {
                     checkIfItemExist(id, item);
                     break;
                 }
                 else
                 {
                     addItemAtEmptySlot(item);
                 }
                 
 
             }
         }
     }
 
     void addItemAtEmptySlot(Item item)
     {
     
         for(int i = 0; i<Items.Count; i++)
         {
             if(Items[i].ItemName == null)
             {
                 Items[i] = item;
                 break;
             }
         }
     }
 
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
0

Answer by jimmycrazyskills · Dec 18, 2018 at 02:05 PM

Hi mate, I'm not sure if this is the best way of doing it, but you could use PlayerPrefs. You can save values to PlayerPrefs and well as read them. PlayerPrefs is just something designed to store data between sessions.

You can save to PlayerPrefs by doing the following (you can also .SetString if you need to save text values, instead of numerical data)

 PlayerPrefs.SetInt("ItemsCount", currentInventoryCount);

Then to read back values you can do the following:

 currentInventoryCount = PlayerPrefs.GetInt("ItemsCount");


Hope this helps abit ;)
See https://docs.unity3d.com/ScriptReference/PlayerPrefs.html for more details.

Comment
Add comment · 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

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

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

Related Questions

Saving my Equipment Slots 1 Answer

System.IO Help! 2 Answers

How to save a file while connected to a server in the right dircetory 0 Answers

Serialization 2 Answers

Saving data. 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