GUI not updating after scene change on IAP button click?
I am having an issue where the PlayerPrefs is not storing the coins collected GUI in my game.
To start off, I have IAP buttons that send people to the Google Play Store to purchase some coins. When the game is first started in the menu where the store is accessed, the player can purchase some coins and they will update the GUI. However, when the player changes to another scene and then goes back to the menu scene to access the store, the GUI is no longer updating on the button click after purchasing more coins from the Google Play Store.
I have the PlayerPrefs to save the information on a button call function, which the function is working on load from start, but not after returning from another scene. When a player clicks on the purchase function on game start, the Debug Logs are active indicating that the button call function is running correctly. However, when a player clicks on the purchase function after going to another scene and going back to the menu, then all of the Debug Logs are not shown and an error is displayed as: Sorry, this product is not defined.
Here is the script for the coins with the button call function.
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.UI;
public class CoinManager : MonoBehaviour
{
public static CoinManager Instance;
public int Coins { get; private set; }
public static event Action<int> CoinsUpdated = delegate {};
[SerializeField]
int INITIAL_COINS = 0;
const string COINS = "COINS"; // key name to store high score in PlayerPrefs
void Awake()
{
if (Instance)
{
//DontDestroyOnLoad(gameObject);
DestroyImmediate(gameObject);
}
else
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
}
// On Start
void Start()
{
Reset();
}
public void Reset()
{
// Initialize coins
Coins = PlayerPrefs.GetInt(COINS, INITIAL_COINS);
Debug.Log ("Reset Coins To 100 Count");
}
public void AddCoins(int amount)
{
Coins += amount;
Debug.Log ("Coins Added To GUI On Button Click From Store __ Logged Before PlayerPrefs");
// Store new coin value
PlayerPrefs.SetInt(COINS, Coins);
Debug.Log ("Coins Added To GUI On Button Click From Store __ Logged After PlayerPrefs");
// Fire event
CoinsUpdated(Coins);
Debug.Log ("Coins Added To GUI On Button Click From Store __ Logged After CoinsUpdated");
}
public void RemoveCoins(int amount)
{
Coins -= amount;
// Store new coin value
PlayerPrefs.SetInt(COINS, Coins);
// Fire event
CoinsUpdated(Coins);
}
}
Here are two pictures of the Debug Log reports:
Answer by dinostudios123 · Oct 18, 2018 at 03:23 AM
Actually I figured it out. I changed (void Start) to (void Update) and now it works great!