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 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
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

0 Replies

· Add your reply
  • Sort: 

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

151 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

Related Questions

Highscrore won't work 1 Answer

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

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