Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dracolyte · Dec 15, 2020 at 10:07 PM · variableplayerprefs

Variable should have a value but if i start the game, the value resets to 0

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);
     
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rhapen · Dec 16, 2020 at 01:21 AM

You do not run PlayerPref.Save() to save values you set and even you load in awake with default value then in every function you overwrite the variable and load as first without default value which will cause it to be 0

 HealthUpgradeCost = PlayerPrefs.GetFloat("HealthUpgradeCost");



But another important point to quote https://answers.unity.com/questions/1325056/how-to-use-playerprefs-2.html " Answer by Commoble · Mar 13, 2017 at 01:25 AM

Playerprefs is meant for saving player preferences; you wouldn't want to use it to save high scores and progress data, because it's not difficult for players to edit the playerprefs data.

There's a couple of ways to save data safely, and this official unity tutorial video goes over one of them, using C#'s BinaryFormatter. "

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Highscrore won't work 1 Answer

PlayerPrefs doesn't apply value, even if i set the value with the saved PlayerPrefs 0 Answers

Player Prefs, Adding Two Variables 1 Answer

Resetting Timer Using PlayerPrefs? (JS) 1 Answer

What is Wrong With The Logic in My Variable? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges