playerpreferance.setint problem

I am develop one game.In this game 15 levels. I am face this type of issues when send one level score into second levels. I am used player preferences in start() method. Get after Increase the score 20% levels. Player will be given score when the levels finished success.

how to score get used preferences in next level??

Math: Level 1 would be 20 + 0 Level 2 is 20 + (20), Level 3 is 20 + (40), Level 4 is 20 + (60), Etc. ex:

Scene1-> Level1(finishlevel) -> Getclaim button press currentreward=20;

Scene2->Level2(finishlevel)-> Getclaim button press currentreward=40;



    int pointAdd;
    public int levelID;   //ex: Leve1 levelId=0 ,Level2 Leveld=1
    public Text creditsText;

    void FixUpdate()
       creditsText.text="" + PlayerPrefs.GetInt("", 0).ToString();

    public void GetClaim()
        pointAdd = PlayerPrefs.GetInt("",0);
        pointAdd = 20 + (levelID * 20); //FIRST SCENE LEVEID=0 //SecondScene LevelId =1 ////problem here when i m quit a game not increment 20,game store only 20
        PlayerPrefs.SetInt("", pointAdd);    
        creditsText.text = ("" + pointAdd);
        btnreward.SetActive(false);   //one time claim then deactive button

what i m doing: playing a game when i m play level1=20,level2=40 preferance store.that run successful but when i m quit a game and again play a game(means level1)current credittextis=40 but not add a 20 means my credittext is=60,but game can store here Credittext=20??

  • \$\begingroup\$ Can you edit your sequence of events above to indicate when you think Start() is called in this chain? This may clarify how you expect this to work, versus what's actually happening. \$\endgroup\$
    – DMGregory
    Jun 1 '19 at 13:14
  • 1
    \$\begingroup\$ If you know what level it is, then can't you figure out the score? Or is the score modified in a difference part of the code? If anything else uses score, please include it in your question. \$\endgroup\$
    – Alex F
    Jun 1 '19 at 19:04

PlayerPrefs.SetInt(name_key, value); saves your value with the name key specified in a string.

value = PlayerPrefs.GetInt(name_key); gets the value which was previously saved with SetInt function, and sets it back to value.

So when you want to save your score you have to do this:

PlayerPrefs.SetInt("score", pointAdd);

But then for loading it into the second level, you have to do this:

pointAdd = PlayerPrefs.GetInt("score");

And finally, if you want to check if there is a score saved in your PlayerPrefs, you have to do this:


Which returns a boolean.

For more information:

list of PlayerPrefs functions: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Set Int : https://docs.unity3d.com/ScriptReference/PlayerPrefs.SetInt.html

Get Int : https://docs.unity3d.com/ScriptReference/PlayerPrefs.GetInt.html

Has Key: https://docs.unity3d.com/ScriptReference/PlayerPrefs.HasKey.html


I didn't quite catch what you were trying to achieve, but you can have these functions to help you:

void SaveScore(int score){
    PlayerPrefs.SetInt("score", score);
int LoadScore(int defaultValue = 20){
    return PlayerPrefs.GetInt("score", defaultValue);

The LoadScore function is equivalent to this:

int LoadScore(int defaultValue = 20){
        return PlayerPrefs.GetInt("score");
    return defaultValue;
  • \$\begingroup\$ thanks,given answer but something wrong in my program pointAdd = PlayerPrefs.GetInt("score"); this line add in getclaim button, ` PlayerPrefs.SetInt("score", pointAdd);` this line add in start(). \$\endgroup\$ Jun 2 '19 at 13:30
  • \$\begingroup\$ preferance stored but when i m press getclaim button that current level not show added score,but added score will be show in next level?? \$\endgroup\$ Jun 2 '19 at 13:55
  • \$\begingroup\$ @rahulpatil can you copy all your code and put it in an edit like mine in your question? \$\endgroup\$ Jun 2 '19 at 13:59
  • \$\begingroup\$ Just as an FYI: do not use PlayerPrefs for storing game state. Highscores is probably fine, but just realize that a player can directly edit the player prefs file and change any value to any other value that they want. The file is intended for things like keybinds, volume settings, and so on. Things that are player preferences. \$\endgroup\$ Jun 2 '19 at 21:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .