- Home /
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);
....
}
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);
}
...
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
$$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 ?