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 can record score in unity

Discussion in 'Scripting' started by gstahsn, Dec 26, 2014.

  1. gstahsn

    gstahsn

    Joined:
    Dec 26, 2014
    Posts:
    11
    Hi.
    How can I do to be saved when the score of the game is rebooted.
    When the user is off his game score will be recorded.
    Recorded score will stand in place when the game opens.
    How can I do??

    Thanks
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. gstahsn

    gstahsn

    Joined:
    Dec 26, 2014
    Posts:
    11
    Thanks @Dantus Can you give example
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  5. gstahsn

    gstahsn

    Joined:
    Dec 26, 2014
    Posts:
    11
    thanks by
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It is not my intention to be unfriendly. It is okay if you don't know where to start and that you may somehow not find PlayerPrefs. But with that, it is really trivial to find lots of examples.
     
    theANMATOR2b likes this.
  7. jaredic

    jaredic

    Joined:
    Jan 4, 2015
    Posts:
    8
    Best regards from me,here we are all on the same task,to make a good GAME!!!
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Not really, some of us are here for self promotion of our own tutorial, assets, art ect. Some of us just like helping random strangers on the net.
     
  9. jaredic

    jaredic

    Joined:
    Jan 4, 2015
    Posts:
    8
    maybe you are right,but y think dantus try to help...y am new in unity,and it looks ok for me,you have documentation,tutorials,forum where you can learn a lot...best wishes
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,498
    This is the persistent data pattern I like to use in Unity3D:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PersistentSettings
    5. {
    6.     const int DefaultHighScore = 10000;
    7.     const string s_HighScore = "HighScore";
    8.     public int HighScore
    9.     {
    10.         get
    11.         {
    12.             return PlayerPrefs.GetInt( s_HighScore, DefaultHighScore);
    13.         }
    14.         set
    15.         {
    16.             PlayerPrefs.SetInt( s_HighScore, value);
    17.         }
    18.     }
    19. }
    Whenever you want to set a high score at end of game, your code might look like:

    Code (csharp):
    1. if (Score > PersistentSettings.HighScore)
    2. {
    3.     PersistentSettings.HighScore = Score;
    4. }
    You can put any other settings you want in there, like sound on/off, etc.
     
    Dantus likes this.
  11. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Altruism prevails! o/

    on topic: player prefs is definitely the easiest way to save something as basic as a top score. Load it into the program when your game starts, save it after a completed round if it's greater than the previous top.
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Since we are back on topic its worth pointing out that player prefs is stored as plain text. On a PC my three year old daughter could hack into it and change the scores. Totally up to you how secure you want to make it.

    On easy way to quickly stop amateur hackers would be to serialise the score using a binary formatter, and store the resulting string in player prefs (or a file). Not enough to stop a genuine hacker, but it will stump any amateurs.
     
  13. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    hackers are only ruining it for themselves :p
     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    True. But it can be a hell of a pain if you later decide to use the same system for an online leader board.

    Of course this all assumes your game is popular enough to attract people that want to take the time to fake high scores...