- Home /
how to make a currency system
In my game I want a money system however i dont know how to save the amount using playerprefs and how to link my scoremanager to the Money. I have a text element and have created an empty object called TileBuxManager and the money is TileBux. But what i want is for when the score from scoremanager = 100 the TileBux value goes up by 1 and his saved. Here is my code for scoremanager:
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ScoreManager : MonoBehaviour
 {
     public Text ScoreText; 
     private int score;
     public Text highscore;
 
 
     public void IncreaseScore( int increment )
     {
         highscore.text = PlayerPrefs.GetInt("highscore").ToString();
 
         score += increment;
         if (score > PlayerPrefs.GetInt("highscore"))
         {
             PlayerPrefs.SetInt("highscore", score);
             highscore.text = PlayerPrefs.GetInt("highscore").ToString();
         }
 
         ScoreText.text = score.ToString();
     }
 }
 
Answer by I_Am_Err00r · Jul 30, 2019 at 02:08 PM
I'm assuming that in the TileBuxManager script "highscore" is the TileBux (if that is the case, why not change not change the "highscore" string reference to "tilebux"?).
 public class ScoreManager : MonoBehaviour
  {
      public Text ScoreText;
      private int score;
      public Text highscore;
  
  
      public void IncreaseScore( int increment )
      {
          int tileBux = PlayerPrefs.GetInt("highscore")
          highscore.text = tileBux;
  
          score += increment;
          if (score > 100)
          {
              tileBux ++;
              PlayerPrefs.SetInt("highscore", tileBux);
              highscore.text = tileBux;
              score = 0;
          }
          ScoreText.text = score.ToString();
      }
  }
Now with this solution, score will reset to 0, and this will display on the screen that way too; if you want the score to keep incrementing after that, there are a couple ways to go about having every 100th score add a tilebux without resetting, but it depends on if you want that data to persist between scene loads and creating another int to log that score, something like this:
 public class ScoreManager : MonoBehaviour
  {
      public Text ScoreText; 
      private int score;
      private int everyHundred;
      public Text highscore;
      
      private void Start()
 {
 everyHundred= PlayerPrefs.GetInt("EveryHundred");
 }
  public void IncreaseScore( int increment )
       {
           int tileBux = PlayerPrefs.GetInt("highscore")
           highscore.text = tileBux;
   
           score += increment;
           everyHundred += increment;
           if (everyHundred > 100)
           {
               tileBux ++;
               PlayerPrefs.SetInt("highscore", tileBux);
               highscore.text = tileBux;
               everyHundred = 0;
           }
           ScoreText.text = score.ToString();
       }
And then on disable or whatever have:
  PlayerPrefs.SetIn(EveryHundred), everyHundred);
So it saves it between scenes.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                