So I’ve seemed to have hit a road block with my in app purchases. I have gotten the IAPs to work, however, the saving them after reload is the problem. I have a script called GameManager that handles my scoring and such. I then used the same script to call my gold system as well. So On start up I call gold.playerPrefs.getInt("Gold"); and then inside the save and exit functions I call PlayerPrefs.SetInt("Gold"), gold; so that is saving my gold. Now when I close the game in editor and restart it, it disappears. I’ve read so many posts and watched so many videos for the past two days. I’ve emplemented IAPs is all my games and for some reason I’m missing something… any suggestions will help. Thanks fellow devs!
Your calls are confusing me. what is gold.playerprefs? And your SetInt I assume is a typo.
gold = PlayerPrefs.GetInt(“Gold”);
PlayerPrefs.SetInt(“Gold”, gold);
I would not suggest you use playerprefs to track shop purchased items, but if you must, just make sure the calls are correct.
Yeah i was posting from my phone on the run. So let me break it down…
I have Unity IAP installed and integrated into my game. I,m using the Purchaser script that comes from their tutorial. I have implemented my own snippets inside to call on purchases. This is some of what Ive changed inside the Purchaser script.
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
if (String.Equals(args.purchasedProduct.definition.id, Product_50_Gold, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
GoldManager.gold += 50;
PlayerPrefs.SetInt ("Gold", gold);
PlayerPrefs.Save();
}
else if (String.Equals(args.purchasedProduct.definition.id, Product_100_Gold, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
GoldManager.gold += 100;
PlayerPrefs.SetInt ("Gold", gold);
PlayerPrefs.Save();
}
else if (String.Equals(args.purchasedProduct.definition.id, Product_500_Gold, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
GoldManager.gold += 500;
PlayerPrefs.SetInt ("Gold", gold);
PlayerPrefs.Save();
}
else
{
Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}
return PurchaseProcessingResult.Complete;
}
Now this calls on my GoldManager Script. which looks like this…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GoldManager : MonoBehaviour {
public static int gold;
Text text;
void Awake ()
{
text = GetComponent <Text> ();
Load ();
}
void Update ()
{
text.text = "Gold: " + gold;
}
void Load()
{
gold = PlayerPrefs.GetInt ("Gold", 0);
}
}
This this sets the text to Gold: amount purchased. This all works just fine… I can buy within the store and it all works fine, except i don’t get why its not saving and loading… Ive added gold = PlayerPrefs.GetInt ("Gold", 0);
to my start functions and added PlayerPrefs.SetInt ("Gold", gold); to the quit functions of my gamemanager. So when the game closes its saving the gold amount. Like i said the purchase part works fine, saving and loading doesn’t. Am i placing the player.prefs in the wrong areas? i believe that the start and quit functions would be appropriate…
Your purchase script is saving “gold”, but what is the value of gold at that point? Are you not trying to save GoldManager.gold?
I set the function so that when ever they buy a gold back, it will add which ever amount to the gold handler. So by saying GoldManager.gold +=100 will add 100 gold to their gold count… then I added PlayerPrefs.SetInt("Gold", gold); to save it to the gold instance. Are you saying I’m saving it wrong? It’s the same way over set up scoring. I call my score manager after the score a point… it then adds the point with score += 1, and then I save it with the same playerPrefs except it’s PlayerPrefs.SetInt("Score", score); and that works just fine. Then I have a function to save best score based of what the current high score is. Along with some math variables like if it’s greater than and all that snazz. The scores save just fine after I close the game. But my coins dissapear. I’m wondering if the Purchaser script or unity iap has something to do with it.
PlayerPrefs.SetInt(“Gold”, gold); What is gold? I think what you are missing is how is gold referencing the gold value in your GoldManager script? Your purchase script must have a variable gold, but how is that gold value pointing at your goldManager gold?
PlayerPrefs.SetInt(“Gold”, GoldManager.gold); should be correct as it points to the value in your GoldManager and not to a value in your purchase script.(I can’t see your purchase script, so my guess is you aren’t updating that value, which you shouldn’t need to since you have it in your GoldManager);
I think it’s more of what is “Gold”. My gold is an integer… that’s being called as public int gold; within my gold manager class and it’s what set my gold amount after purchase. Same with my score. I have my score set to my game manager and it calls with start as follows…
avoid Start()
{
bestScore = PlayerPrefs.GetInt("BestScore", 0);
}
I stated in the beginning of my script it will be public int score;
Then I have a function that calls save data as follows…
void SaveData()
{
if(score > bestScore)
{
bestScore = score;
PlayerPrefs.SetInt("BestScore", bestScore);
}
}
This saves best score when ever I quit the game or go to the main menu. I also have score=0 in my quit function so that my score resets on quit. That all functions correctly. bestScore is an int as well. As is gold. That is why I call gold in my player pref cause it’s referring to the gold integer.
I see what your saying with the gold though. But even in my game manager I call on score and that is referenced inside my score manager. Both score manager and gold manager scripts are the same other than the referenced names I shouldn’t have to reference goldmanager.gold within my purchase script as I don’t in my gamemager script
Honestly, I can’t see your scripts to know how they are setup. I can only tell you what is probably happening from what I see. If you have
public int gold;
In your purchase script, it is not the same as the gold in your GoldManager script. So if you are saving your playerpref by saving the gold in your purchase script and it never changes in value, it will always be the same.
Unity has no way of knowing that you want to save your GoldManager.gold value and not the gold int in your purchase script.