The error may be simple, but I’m bad at coding. I’m making an upgrade system for my game. The problem is, that the cost of the upgrade is set in the code to 400 but if i start the game, it changes to zero. How do I fix the cost? Btw sorry for the long code
//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");
//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++;
Debug.Log(HealthUpgradeCost);
//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("AllMoney", _playerStats.AllMoney);
}
public void UpgradeDamage()
{
//Get the saved values
DamageUpgradeCount = PlayerPrefs.GetInt("DamageUpgradeCount");
DamageUpgradeCost = PlayerPrefs.GetFloat("DamageUpgradeCost");
//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("AllMoney", _playerStats.AllMoney);
}
public void UpgradeCritChance()
{
//Get the saved values
CritChanceUpgradeCount = PlayerPrefs.GetInt("CritChanceUpgradeCount");
CritChanceUpgradeCost = PlayerPrefs.GetFloat("CritChanceUpgradeCost");
//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("AllMoney", _playerStats.AllMoney);
}
public void UpgradeCritMultiplier()
{
//Get the saved values
CritMultiplierUpgradeCount = PlayerPrefs.GetInt("CritMultiplierUpgradeCount");
CritMultiplierUpgradeCost = PlayerPrefs.GetFloat("CritMultiplierUpgradeCost");
//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("AllMoney", _playerStats.AllMoney);
}
}