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 (Saving Data Between Scenes in Unity — SitePoint) 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);
....
}