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 greens356 · Jun 07, 2020 at 06:32 AM · inventoryscene-switchingdontdestroyonloadinventory systempersistence

How to persist player inventory using dictionary between scenes

Hello all,

Apologies for the formatting of this post, I can't seem to get the line breaks working?

I am trying to follow this tutorial (https://www.sitepoint.com/saving-data-between-scenes-in-unity/) about keeping game data between scenes and I see that the difference between what the tutorial is doing (saving individual variables using the GlobalContoller) and what I am doing (using a Dictionary) but essentially I think I am doing the same thing?

I am noticing that when I load a new scene it is considering my dictionary to be null and I have code to catch the null and create a new dictionary and populate it with one entry (an empty list of game objects with its associated key string). The issue is I don't understand how to declare and instantiate the dictionary in the first scene and then just keep using the same dictionary throughout the rest of the game between scenes, which is what I thought DontDestroyOnLoad() was supposed to do for me? It feels like a chicken and egg problem but I am sure I am missing something.

What I would like to have is just to use this Dictionary for all scenes and to keep it between scenes and items can be added and removed from it independent of scene.

Here is what I have in code:

PersistentGameInfo.cs

 public class PersistentGameInfo : MonoBehaviour
 {
     public static PersistentGameInfo instance;
 
     public Dictionary<string, List<GameObject>> playerInventory;
 
 ....
 
     private void Awake()
     {
         if (instance == null)
         {
             DontDestroyOnLoad(gameObject);
             instance = this;
         }
         else if (instance != this)
         {
             Destroy(gameObject);
         }
 }
 
 ....
 
     public Dictionary<string, List<GameObject>> GetInventoryList()
     {
         if (playerInventory == null)
         {
             Dictionary<string, List<GameObject>> newInv = new Dictionary<string, List<GameObject>>();
             newInv.Add("NES", new List<GameObject>());
             return newInv;
         }
         return playerInventory;
     }
 
     public void AddInventoryItems(string type, List<GameObject> itemList)
     {
         //TODO: Use enums for consoles..
         if (type.Equals("NES")) {
             playerInventory.TryGetValue("NES", out List<GameObject> nesList);
             foreach (GameObject item in itemList)
             {
                 nesList.Add(item);
             }
         }
     }
 .....
 }
 
 **TabletMenuController.cs**
   
   ...
     void Start()
     {
       ...
         playerInventory = PersistentGameInfo.instance.GetInventoryList();
      ...
     }
 
 ...
 
  void UpdateExpandedList(Collider button)
     {
                 
          ...
                 playerInventory = PersistentGameInfo.instance.GetInventoryList();
 
           ...
                 if (playerInventory != null)
                 {
                     playerInventory.TryGetValue("NES", out List<GameObject> nesGames);
                     foreach (GameObject game in nesGames)
                     {
                         GameObject newGame = Instantiate(inventoryItem, NESGameListContent.transform);
                         newGame.GetComponent<TMPro.TextMeshProUGUI>().text = game.GetComponent<SaleItem>().itemName;
 
               ...
 
             }
 
 
   
 **PurchaseItems.cs**  
   
   
    public void OnClickSubmitOffers()
     {
     ...
  if (totalOfferAmt >= totalPriceAmt - (totalPriceAmt * percentDiscount))
             {
                 PersistentGameInfo.instance.AddInventoryItems("NES", purchasableGameItems);
 ....
 }
 














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

2 Replies

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

Answer by greens356 · Jun 09, 2020 at 06:07 AM

Ok so I needed to add the DontDestroyOnLoad() method to the SaleItem.cs script in the awake method (this script is the class that handles the things I want to persist between scenes). The issue was that when I changed scenes, the game Objects I was referencing in my lists were being nulled out because they were being destroyed when leaving the scene and the lists were only holding a reference to that game object which is now destroyed, therefore error. So this persisted my inventory, but now I have an issue where everything that is in the scene I am leaving is now following me to the next scene, which is what I don't want, so I am going to write some code to then Destroy all the items that I do not want to persist between scenes ( a subset of all total items from scene I am leaving meaning Items in scene that are not in my inventory at time of leaving). Hope the added explanations help someone else out there!

SaleItem.cs

...

     private void Awake()
     {
         DontDestroyOnLoad(transform.gameObject);
     }

...

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

Answer by unity_qrj_5UYMolP0jA · Jun 07, 2020 at 07:09 AM

Does this help? https://answers.unity.com/questions/460727/how-to-serialize-dictionary-with-unity-serializati.html

Comment
Add comment · Show 1 · 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 greens356 · Jun 07, 2020 at 10:34 PM 0
Share

$$anonymous$$aybe, I am not sure. I don't understand Scriptable Objects enough yet to deter$$anonymous$$e whether its better to use those (and therefore I think getting rid of DontDestroyOnLoad() ?) or to try to make the Dictionary object for my inventory work with this hacky serialization stuff. Do you have any thoughts @unity_qrj_5UY$$anonymous$$olP0jA ?

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

131 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 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

How i can make a script-made button interactable? 0 Answers

sending information to a function in another script to use 2 Answers

Need help with my 2D inventory slot system 1 Answer

using a rigidbody or softbody backpack as an inventory 0 Answers

GUI Elements Carry Over To Next Scene 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