- Home /
Question by
Elmdran · Jan 04, 2015 at 04:35 PM ·
c#playerprefssavingdatapublic variable
Saving public variable data set in inspector?
Hi. I'm making an Incremental Game (think cookie clicker) and I have a script on each item that you can buy with public variables, each button or each upgrade has this script, and the prices, count, power increase values are set in the inspector. But how and what is the best way to save this data?
I tried doing this:
public class ItemManager : MonoBehaviour {
public UnityEngine.UI.Text itemInfo;
public Click click;
public float cost;
public int tickValue;
public int count;
public string itemName;
private float baseCost;
void Awake(){
cost = PlayerPrefs.GetFloat ("ItemCost");
tickValue = PlayerPrefs.GetInt ("ItemTick");
count = PlayerPrefs.GetInt ("Amount");
}
void Start(){
baseCost = cost;
}
public void PurchasedItem(){
if (click.gold >= cost) {
click.gold -= cost;
count += 1;
cost = Mathf.Round (baseCost *
Mathf.Pow(1.15f, count));
}
}
void OnApplicationQuit(){
PlayerPrefs.SetFloat ("ItemCost", cost);
PlayerPrefs.SetInt ("ItemTick", tickValue);
PlayerPrefs.SetInt ("Amount", count);
}
}
But when I start my game the buttons say, cost 0, power 0, everything is set to 0. I need help!
Comment
Answer by TrungNguyenNgoc · Jan 19, 2016 at 08:58 AM
check is the any PlayerPrefs.DeleteAll(); if yes, just comment them.