- Home /
Problems with PlayerPrefs or something else
So i am a COMPLETE noob at this, i followed a few tutorials on how to make a clicker game and then made the rest by myself. I was trying to make it so the game saves all my stats with playerprefs (money, money per second e.t.c) I got everything to save except some of my upgrade buttons. I couldn't make it so it saves their count or their price. I got it to work on my other upgrade buttons ( the ones that add money per sec), but not for the ones that add money per click ( i tried saving them like i saved my other upgrade buttons, because the code for them is almost identical). I will add the scripts of my buttons ( both types ) and how i save the data. I know i am probably missing something obvious and i'm sorry if i am. Thanks!
Also the game is a joke and that's why some variables are called "depression" :D
The Script Of How I Save And Load The Buttons:
using UnityEngine;
using System.Collections;
public class testt : MonoBehaviour
{
public Click click;
public ItemManager[] items;
public UpgradeManager[] items2;
public void load()
{
float olddepression = PlayerPrefs.GetFloat("depression");
click.depression = olddepression;
int olddpc = PlayerPrefs.GetInt("dpc");
click.depressionperclick = olddpc;
foreach(ItemManager item in items)
{
item.count = PlayerPrefs.GetInt(item.itemName);
}
foreach (UpgradeManager item in items2)
{
item.count = PlayerPrefs.GetInt(item.upgradeName);
}
foreach (UpgradeManager item in items2)
{
item.cost = PlayerPrefs.GetFloat(item.upgradeName + "2");
}
foreach (ItemManager item in items)
{
item.cost = PlayerPrefs.GetFloat(item.itemName + "2");
}
}
public void save()
{
float olddepression = click.depression;
PlayerPrefs.SetFloat("depression", olddepression);
float olddpc = click.depressionperclick;
PlayerPrefs.SetFloat("doc", olddpc);
foreach (ItemManager item in items)
{
PlayerPrefs.SetInt(item.itemName, item.count);
}
foreach (ItemManager item in items)
{
PlayerPrefs.SetFloat(item.itemName + "2", item.cost);
}
foreach (UpgradeManager item in items2)
{
PlayerPrefs.SetInt(item.upgradeName, item.count);
}
foreach (UpgradeManager item in items2)
{
PlayerPrefs.SetFloat(item.upgradeName + "2", item.cost);
}
}
}
The Script Of The Buttons I Don't Know How To Save:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UpgradeManager : MonoBehaviour {
public Click click;
public UnityEngine.UI.Text upgradeInfo;
public float cost;
public int count;
public int clickPower;
public string upgradeName;
public Color standart;
public Color affordable;
private float baseCost;
private Slider _slider;
void Start(){
baseCost = cost;
_slider = GetComponentInChildren<Slider> ();
}
void Update() {
upgradeInfo.text = upgradeName + "\nCost: " + CurrencyConverter.Instance.GetCurrencyIntoString(cost, false, false) + "\nPower: +" + CurrencyConverter.Instance.GetCurrencyIntoString(clickPower, false, false);
if (click.depression >= cost) {
GetComponent<Image> ().color = affordable;
} else {
GetComponent<Image> ().color = standart;
}
_slider.value = click.depression / cost * 100;
}
public void PurchasedUpgrade() {
if (click.depression >= cost) {
click.depression -= cost;
count += 1;
click.depressionperclick += clickPower;
cost = Mathf.Round (baseCost * Mathf.Pow (1.15f, count));
}
}
}
The Script Of The Buttons That I Managed To Save:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ItemManager : MonoBehaviour {
public UnityEngine.UI.Text itemInfo;
public Click click;
public float cost;
public int tickValue;
public int count;
public string itemName;
public Color standart;
public Color affordable;
private float baseCost;
private Slider _slider;
void Start(){
baseCost = cost;
_slider = GetComponentInChildren<Slider> ();
}
void Update(){
itemInfo.text = itemName + "\nCost: " + CurrencyConverter.Instance.GetCurrencyIntoString(cost, false, false) + "\nDepression: " + CurrencyConverter.Instance.GetCurrencyIntoString(tickValue, false, false) + "/s";
_slider.value = click.depression / cost * 100;
if (click.depression >= cost) {
GetComponent<Image> ().color = affordable;
} else {
GetComponent<Image> ().color = standart;
}
}
public void PurchasedItem(){
if (click.depression >= cost) {
click.depression -= cost;
count += 1;
cost = Mathf.Round (baseCost * Mathf.Pow (1.15f, count));
}
}
}
?
in the first script
foreach (Upgrade$$anonymous$$anager item in items2)
{
item.count = PlayerPrefs.GetInt(item.upgradeName);
}
is that what you meant?
or do you mean from where i call the function? if so i call it from an autosave script which saves the data every second, but ive tried to call it from many things before but the upgrade buttons still dont work.
if you need the autosave script just ask :)
Answer by danyx1980 · Dec 14, 2017 at 09:29 PM
itemName and upgradeName are strings but you're loading and saving them as if they were float or int.
@danyx1980 I know theye are strings im using them as strings aswell because playerprefs is playerprefs.getfloat(string, value) but because i want to save every item in the array ins$$anonymous$$d of typing a string in the string part i type item.itemName which is a string and each item has a different name so i can use that as the name of the key for playerprefs But thanks anyways :) also the one with itemName works fine just not the upgradeName one
Your answer
![](https://koobas.hobune.stream/wayback/20220612144611im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Issues creating proper level-unlocking script 0 Answers
How to load an inventory array? 1 Answer
Can someone tell me what is wrong with this? 2 Answers
Player Prefs Mixed Array 1 Answer
How do to you save time for each player and display it? 2 Answers