Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How To Save Scores?

Discussion in '2D' started by Dogg, Jul 10, 2014.

  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Now this question has been asked billions of times on this website, so I'm well aware that there are plenty of source materials around here that I can use. However, my mind has not been working correctly as of lately and I'm having a hard time understanding how to save scores. I know to use PlayerPrefs and all that but the problem is writing the correct code for my scripts(the way they are structured is different than anyone else). So here's the question, how can I save the score I get after my Player dies and continue to save after I get an even higher score? So like let's say I get 100 points, and it saves it. Then I can view it in the Menu and see my best score. Then the next run I get 200 points, and it overwrites the previous 100 score and I can view it again in the Menu where I will now see that it says 200 instead of 100. I have an idea on how to view it in the menu and such, but not how I can actually save the scores. So if anyone can answer this question, please do.

    Here are my scripts so you know how my code is structured:

    Score Script(it goes on the Main Camera of the Game Scene): http://pastebin.com/brrcBn1W

    GameOverScript(It goes on the Main Camera of a different scene): http://pastebin.com/cfF7HF2M

    As you can tell if you look at the GameoverScript, I already started trying to save my scores that I get, but it hasn't worked so far(at least I don't think).
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    I recommend putting a Debug.Log in your OnDisable, so you can be sure that's getting run. Then, I would take that comparison code out of Update - Update is for things you want to run every single frame. Maybe put it in Start, instead?
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Okay so I put a debug.log in the OnDisable, and it works after I die and the game over screen pops up, so that's good. As for putting the comparison code out of Update, I tried to put it in Start but that didn't seem to do anything. Everything was the same as it always is. I assume you were talking about the "if (score > highscore) {", but I'm not too sure. When I die and get a score, it shows me the score and then I press retry, then I get a lower score and it just shows the lower score, which is great and all but how can I save or check if the game actually saves the highest score? Thanks for helping me by the way.
     
  4. cbothra

    cbothra

    Joined:
    Mar 14, 2014
    Posts:
    125
    >>how can I save or check if the game actually saves the highest score?

    For checking, I think you shall at first quit the game after saving the last high score and then when you again play the same you can check that via PlayerPrefs.GetInt("High Score"). If it returns the last saved value then it means that your scores are being saved :)
     
  5. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Sounds like it's not doing anything in Update, either. Maybe stick a Debug.Log in Start? Basically every place you read or write with PlayerPrefs.
     
  6. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    "If it returns the last saved value then it means that your scores are being saved." Now if you mean if a debug works, then yes it seems like it saves it, but if you mean if the score is the same as it was before I quit, then no it doesn't work. Not sure what you mean't by that. I put a debug under highscore = PlayerPrefs.GetInt("High Score");, and it popped up when I died and went to game over. So was I supposed to do that? Or the other thing that I just mentioned?
     
  7. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I just tried it, you're right. The Update does not work, the other places where the other PlayerPrefs are do work though. So moving the PlayerPrefs in the Update to the Start seems like a good idea, but it doesn't work either even with debugging.
     
  8. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    What exactly is happening? Is Start not running? Are the values not coming out? Are the values not comparing correctly?
     
  9. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I'm basing the PlayerPrefs not working by Debugging it. Meaning if the debug does not work, then it itself does not work. The only one that doesn't work seems to be the code in the Update() function, since there's no debug at all during and after the game. So I'm trying to figure out why this piece of code does not appear in the console. So I'm unsure if I should keep this code or not.
     
  10. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    So, Update isn't running at all? That's a little odd, what could be causing it? Are you disabling that object? Doing something with Time?

    That being said... I still think Update is the completely wrong place for that piece of code. Why would you ever want to check the high score every single frame? It belongs in Start, doesn't it? If I understand what's going on.
     
  11. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I put it in Start(), it did not work just like the Update. However, I did another test, this time I made it my goal to get a higher score on my game(I thought to myself maybe that would work). So I got to over 500 points, died, and then turned off the Play button in Unity. I checked the console and the debug popped up right when I quit. So it does in fact work, I guess I needed to get a higher score...Here's a picture of the debug: http://imgur.com/7Vaizwl And where I put the debug:
    Code (CSharp):
    1. if (score > highscore) { //when player dies set highscore = to that score
    2.                         highscore = score;
    3.                         PlayerPrefs.SetInt ("High Score", highscore);
    4.                         Debug.Log("Mikki");
    So this is great, but now I need to be able to see the highest score in my HighScore menu, located in the Main Menu. Do you have an idea on how I can accomplish this? Thanks a bunch Pyrian.