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 yusufalp727 · Aug 05, 2021 at 09:13 AM · playerprefssaveloadscoregameover

Playerprefs When the value I set is zero, there is a 2x increase in the part I get. when i close and reopen the game it only shows the last value. How can ı solve these?

Hello! With Playerprefs, I save the gold and score in the game scene and upload them to the Menu scene, and when Game Over, the Game Over panel opens and the total gold of the current game is displayed. But if he doesn't win any gold in the game, the Game Over panel writes the gold amount of the previous game where it should be 0, and it should not add anything to the total gold in the Menu scene, but the amount of gold doubles. Another problem is that his level progresses as much as the score he earned in the game and this is shown on the Menu scene, but when I turn the game on and off completely, the score earned in the last game is shown in the score section in the Menu scene. Problems: 1- When I earn 0 gold in the game, instead of writing 0 gold in the Game Over panel, the gold of the previous game is written and the place where I kept the total gold in my Menu scene doubles. 2- When I close the game completely and open it, the place where I keep the score in my menu scene will only show the score in the last game instead of showing the total score earned in all games. How can I solve these problems? I need your help! I have 3 scripts:

PlayerControl script:

 public class PlayerControl : MonoBehaviour
 {
    private int gold;
    public Text goldText;
 }
 
  private void OnCollisionStay(Collision collision)
      {
          if (collision.gameObject.tag == "Gold")
          { 
             PlayerPrefs.SetInt("Gold", gold);
             PlayerPrefs.Save();
             gold ++;
             goldText.text="Gold: " + gold;
             Destroy(collision.gameObject); 
          }
      }

Game Manager Script:

 public class GameManager : MonoBehaviour
 {
     public Text scoreText;
     public Text goldTextGOPanel;
     public int goldGOPanel;
 
     private float score;
     private float scoreTime;
 {
 
 void Start()
     {     
         scoreTime=1;
     }
 
 void Update()
     {
         goldGOPanel=PlayerPrefs.GetInt("Gold");           
         goldTextGOPanel.text="Gold: " + goldGOPanel;
 
          if (playerControlScript.gameOver==false)
         {
            scoreText.text=((int)score).ToString();
            score += scoreTime * Time.deltaTime; 
            PlayerPrefs.SetFloat("score", score);
          }
      }
 }

MenuControl script:

 public class Menu : MonoBehaviour
 {
    public Text goldText;
    public Text ScoreText;
    public static int gold;
    public float highScore;
    public static int levelNumber;
    public static float scoreAmount;
    public int scoreGoal;
 
    void Start() 
  {
      //level system
         PlayerPrefs.SetFloat("scoreTotal",PlayerPrefs.GetFloat("score") + scoreAmount);
         scoreAmount=PlayerPrefs.GetFloat("scoreTotal"); 
         levelNumber=1;
         if (scoreAmount>=scoreGoal)
         {
             levelNumber++;
         }
         scoreGoal=levelNumber*100;
         PlayerPrefs.SetInt("levelNumber", levelNumber);
         ScoreText.text=((int)scoreAmount).ToString() + "/" + scoreGoal.ToString();
         LevelNumberText.text=levelNumber.ToString(); 
 
        //total gold amount
         PlayerPrefs.SetInt("GoldTotal",PlayerPrefs.GetInt("Gold") + gold);
         gold=PlayerPrefs.GetInt("GoldTotal");           
         goldText.text="Gold: " + gold; 
    }
 }
     


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 Casiell · Aug 05, 2021 at 12:08 PM

You never initialize your gold value with zero. The only time you update the value is when picking up gold, so if you pick nothing, the value won't be updated.

Add a Start method in your PlayerControl class with this line

 PlayerPrefs.SetInt("Gold", 0);
Comment
Add comment · Show 1 · 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
avatar image yusufalp727 · Aug 05, 2021 at 12:18 PM 0
Share

when I add this line, the total gold value in my Menu scene increases by the amount before adding this line, even if the situation is nonzero.

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

134 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

Related Questions

Problems with saving/loading score with PlayerPrefs [C#] 1 Answer

PlayerPrefs 2 Answers

How can I continue the game without losing values after Game Over? 1 Answer

Save/load playerprefs 2 Answers

[Solved] PlayerPrefs Vector 3 Defaulting to 0,0,0 1 Answer


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