- Home /
Question by
Dracolyte · Dec 16, 2020 at 10:01 PM ·
variableplayerprefs
PlayerPrefs doesn't apply value, even if i set the value with the saved PlayerPrefs
Im having again problems with PlayerPrefs and cant find a solution. If I upgrade and save the value (In script2), e.g. the playerDmg (in script1) should load the value and apply it to playerDmg. This is what it doesnt do. It just takes the default value and ignores the saved. I tried removing the default value but then it just takes zero. How to fix that?
script1:
//Player's Stat's
[HideInInspector] public float
//Things I want so save
playerHp = 100f,
MaxPlayerHp = 100f,
playerDmg = 1f,
critDmgMultiplier = 1f,
critChance = 0.5f,
money = 0f,
AllMoney = 0f,
highscore = 0f,
MinPlayerHp = 0f,
HpDecreasePerSec = 15f,
HpBackOnHit = 0f,
HpBackOnKill = 20f,
score = 0f;
private void Awake()
{
playerHp = PlayerPrefs.GetFloat("playerHp", 100f);
playerDmg = PlayerPrefs.GetFloat("playerDmg", 1f);
critDmgMultiplier = PlayerPrefs.GetFloat("critDmgMultiplier", 1f);
critChance = PlayerPrefs.GetFloat("critChance", 1f);
AllMoney = PlayerPrefs.GetFloat("AllMoney", 1000000f);
highscore = PlayerPrefs.GetFloat("highscore", 0f);
Debug.Log(playerDmg);
}
}
script2:
//upgrade count text
public Text HealthUpgradeText;
public Text DamageUpgradeText;
public Text CritChanceUpgradeText;
public Text CritMultiplierUpgradeText;
//Cost text
public Text HealthUpgradeCostText;
public Text DamageUpgradeCostText;
public Text CritChanceUpgradeCostText;
public Text CritMultiplierUpgradeCostText;
//How much
private float HealthUpgradeValue = 5f;
private float DamageUpgradeValue = 1f;
private float CritChanceUpgradeValue = 0.5f;
private float CritMultiplierUpgradeValue = 1f;
//How much upgraded
private int HealthUpgradeCount;
private int DamageUpgradeCount;
private int CritChanceUpgradeCount;
private int CritMultiplierUpgradeCount;
//Maximal Upgrades
private const int HealthUpgradeMax = 100;
private const int DamageUpgradeMax = 100;
private const int CritChanceUpgradeMax = 50;
private const int CritMultiplierUpgradeMax = 100;
//How much which upgrade cost
private float HealthUpgradeCost = 300f;
private float DamageUpgradeCost = 300f;
private float CritChanceUpgradeCost = 300f;
private float CritMultiplierUpgradeCost = 300f;
private void Awake()
{
//Playerstats
_playerStats = FindObjectOfType<PlayerStats>().GetComponent<PlayerStats>();
AllMoneyText1 = FindObjectOfType<AllMoneyText1>().GetComponent<AllMoneyText1>();
//Load the upgrade count
HealthUpgradeCount = PlayerPrefs.GetInt("HealthUpgradeCount", 0);
DamageUpgradeCount = PlayerPrefs.GetInt("DamageUpgradeCount", 0);
CritChanceUpgradeCount = PlayerPrefs.GetInt("CritChanceUpgradeCount", 0);
CritMultiplierUpgradeCount = PlayerPrefs.GetInt("CritMultiplierUpgradeCount", 0);
//Load costs
HealthUpgradeCost = PlayerPrefs.GetFloat("HealthUpgradeCost", 300f);
DamageUpgradeCost = PlayerPrefs.GetFloat("DamageUpgradeCost", 300f);
CritChanceUpgradeCost = PlayerPrefs.GetFloat("CritChanceUpgradeCost", 300f);
CritMultiplierUpgradeCost = PlayerPrefs.GetFloat("CritMultiplierUpgradeCost", 300f);
//Load the Texts
HealthUpgradeText.text = HealthUpgradeCount + "/" + HealthUpgradeMax;
HealthUpgradeCostText.text = HealthUpgradeCost + "$";
DamageUpgradeText.text = DamageUpgradeCount + "/" + DamageUpgradeMax;
DamageUpgradeCostText.text = DamageUpgradeCost + "$";
CritChanceUpgradeText.text = CritChanceUpgradeCount + "/" + CritChanceUpgradeMax;
CritChanceUpgradeCostText.text = CritChanceUpgradeCost + "$";
CritMultiplierUpgradeText.text = CritMultiplierUpgradeCount + "/" + CritMultiplierUpgradeMax;
CritMultiplierUpgradeCostText.text = CritMultiplierUpgradeCost + "$";
}
private void Update()
{
}
public void UpgradeHealth()
{
//Get the saved values
HealthUpgradeCount = PlayerPrefs.GetInt("HealthUpgradeCount");
HealthUpgradeCost = PlayerPrefs.GetFloat("HealthUpgradeCost", 300f);
//Check if money too low or max upgrade count reached
if (_playerStats.AllMoney < HealthUpgradeCost || HealthUpgradeMax <= HealthUpgradeCount)
return;
//Increase stats
_playerStats.playerHp += HealthUpgradeValue;
_playerStats.MaxPlayerHp += HealthUpgradeValue;
//Subtract money
_playerStats.AllMoney -= HealthUpgradeCost;
//Increase cost and count
HealthUpgradeCost += 50;
HealthUpgradeCount++;
//Change text
AllMoneyText1.moneyText.text = _playerStats.AllMoney + "$";
HealthUpgradeText.text = HealthUpgradeCount + "/" + HealthUpgradeMax;
HealthUpgradeCostText.text = HealthUpgradeCost + "$";
//Save new values
PlayerPrefs.SetFloat("HealthUpgradeCost", HealthUpgradeCost);
PlayerPrefs.SetInt("HealthUpgradeCount", HealthUpgradeCount);
PlayerPrefs.SetFloat("playerHp", _playerStats.playerHp);
PlayerPrefs.SetFloat("AllMoney", _playerStats.AllMoney);
}
public void UpgradeDamage()
{
//Get the saved values
DamageUpgradeCount = PlayerPrefs.GetInt("DamageUpgradeCount");
DamageUpgradeCost = PlayerPrefs.GetFloat("DamageUpgradeCost", 300f);
//Check if money too low or max upgrade count reached
if (_playerStats.AllMoney < DamageUpgradeCost || DamageUpgradeMax <= DamageUpgradeCount)
return;
//Increase stats
_playerStats.playerDmg += DamageUpgradeValue;
//Subtract money
_playerStats.AllMoney -= DamageUpgradeCost;
//Increase cost and count
DamageUpgradeCost += 50;
DamageUpgradeCount++;
//Change text
AllMoneyText1.moneyText.text = _playerStats.AllMoney + "$";
DamageUpgradeText.text = DamageUpgradeCount + "/" + DamageUpgradeMax;
DamageUpgradeCostText.text = DamageUpgradeCost + "$";
//Save new values
PlayerPrefs.SetFloat("DamageUpgradeCost", DamageUpgradeCost);
PlayerPrefs.SetInt("DamageUpgradeCount", DamageUpgradeCount);
PlayerPrefs.SetFloat("PlayerDmg", _playerStats.playerDmg);
PlayerPrefs.SetFloat("AllMoney", _playerStats.AllMoney);
}
public void UpgradeCritChance()
{
//Get the saved values
CritChanceUpgradeCount = PlayerPrefs.GetInt("CritChanceUpgradeCount");
CritChanceUpgradeCost = PlayerPrefs.GetFloat("CritChanceUpgradeCost", 300f);
//Check if money too low or max upgrade count reached
if (_playerStats.AllMoney < CritChanceUpgradeCost || CritChanceUpgradeMax <= CritChanceUpgradeCount)
return;
//Increase stats
_playerStats.critChance += CritChanceUpgradeValue;
//Subtract money
_playerStats.AllMoney -= CritChanceUpgradeCost;
//Increase cost and count
CritChanceUpgradeCost += 50;
CritChanceUpgradeCount++;
//Change text
AllMoneyText1.moneyText.text = _playerStats.AllMoney + "$";
CritChanceUpgradeText.text = CritChanceUpgradeCount + "/" + CritChanceUpgradeMax;
CritChanceUpgradeCostText.text = CritChanceUpgradeCost + "$";
//Save new values
PlayerPrefs.SetFloat("CritChanceUpgradeCost", CritChanceUpgradeCost);
PlayerPrefs.SetInt("CritChanceUpgradeCount", CritChanceUpgradeCount);
PlayerPrefs.SetFloat("critChance", _playerStats.critChance);
PlayerPrefs.SetFloat("AllMoney", _playerStats.AllMoney);
}
public void UpgradeCritMultiplier()
{
//Get the saved values
CritMultiplierUpgradeCount = PlayerPrefs.GetInt("CritMultiplierUpgradeCount");
CritMultiplierUpgradeCost = PlayerPrefs.GetFloat("CritMultiplierUpgradeCost", 300f);
//Check if money too low or max upgrade count reached
if (_playerStats.AllMoney < CritMultiplierUpgradeCost || CritMultiplierUpgradeMax <= CritMultiplierUpgradeCount)
return;
//Increase stats
_playerStats.critDmgMultiplier += CritMultiplierUpgradeValue;
//Subtract money
_playerStats.AllMoney -= CritMultiplierUpgradeCost;
//Increase cost and count
CritMultiplierUpgradeCost += 50;
CritMultiplierUpgradeCount++;
//Change text
AllMoneyText1.moneyText.text = _playerStats.AllMoney + "$";
CritMultiplierUpgradeText.text = CritMultiplierUpgradeCount + "/" + CritMultiplierUpgradeMax;
CritMultiplierUpgradeCostText.text = CritMultiplierUpgradeCost + "$";
//Save new values
PlayerPrefs.SetFloat("CritMultiplierUpgradeCost", CritMultiplierUpgradeCost);
PlayerPrefs.SetInt("CritMultiplierUpgradeCount", CritMultiplierUpgradeCount);
PlayerPrefs.SetFloat("critDmgMultiplier", _playerStats.critDmgMultiplier);
PlayerPrefs.SetFloat("AllMoney", _playerStats.AllMoney);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613021740im_/https://answers.unity.com/themes/thub/images/avi.jpg)