Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

PlayerPrefs.GetInt - save score problem

Discussion in 'Scripting' started by Dexydon, Jan 9, 2016.

  1. Dexydon

    Dexydon

    Joined:
    Dec 20, 2015
    Posts:
    10
    Hello!
    I'm working on my first simple game and this time got problem with save my score:

    First Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScoreScript : MonoBehaviour {
    5.      public int reachedLevel;
    6.     // Use this for initialization
    7.     void Start () {
    8.         reachedLevel = Application.loadedLevel;
    9.         PlayerPrefs.SetInt("lvl", reachedLevel);
    10.     }
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.  
    15.     }
    16.  
    17. }
    18.  

    Second Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUIStart : MonoBehaviour {
    5.     int reachedLevel;
    6.     int bestLevel;
    7.     // Use this for initialization
    8.     void Start () {
    9.         reachedLevel = PlayerPrefs.GetInt ("lvl");
    10.    
    11.  
    12.  
    13.         if (reachedLevel > bestLevel) {
    14.             bestLevel = reachedLevel;
    15.         }
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.  
    21.     }
    22.  
    23.     void OnGUI()
    24.     {
    25.         GUI.Label(new Rect(80, 100, 50, 30), "Score: " + bestLevel);
    26.     }
    27. }
    When I reach level 3 - on main menu I see Score: 3
    But when I try again and reach only level 2 then on main menu I see Score: 2
    I want to save best score.
    How I can do that?
     
  2. treshold

    treshold

    Joined:
    Nov 9, 2013
    Posts:
    225
    you need to store bestLevel too to PlayerPrefs. Now bestLevel is just local variable in second script so line 13 statement is always true.

    Dunno how your game runs but i would do like
    when game ends make it compare reached level with best level and save reached level as best level if its higher and then in second script load best level from playerprefs same way as you load reachedlevel
     
  3. Dexydon

    Dexydon

    Joined:
    Dec 20, 2015
    Posts:
    10
    Still dont know how to do that :(
     
  4. Dexydon

    Dexydon

    Joined:
    Dec 20, 2015
    Posts:
    10
    What do you guys think about that:
    Why still doesn't work?
    Please help :(
    First Script:


    Code (CSharp):
    1.      public int reachedLevel;
    2.     // Use this for initialization
    3.     void Start () {
    4.         reachedLevel = Application.loadedLevel;
    5.         PlayerPrefs.SetInt("lvl", reachedLevel);
    6.     }
    7.     // Update is called once per frame
    8.     void Update () {
    9.  
    10.  
    11.     }
    Second Script:
    Code (CSharp):
    1. public int bestLevel;
    2.     public int newScore;
    3.     // Use this for initialization
    4.     void Start () {
    5.  
    6.         newScore = PlayerPrefs.GetInt ("lvl");
    7.        
    8.  
    9.     }
    10.  
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.         if(newScore > bestLevel)
    15.         {
    16.             bestLevel = newScore;
    17.             PlayerPrefs.SetInt("highscore", bestLevel);
    18.             PlayerPrefs.Save();
    19.            
    20.         }
    21.  
    22.     }
    23.  
    24.     void OnGUI()
    25.     {
    26.         GUI.Label(new Rect(80, 100, 50, 30), "Score: " + bestLevel);
    27.     }
     
  5. Fearleas

    Fearleas

    Joined:
    Dec 24, 2015
    Posts:
    5
    You never set bestLevel to something and in the OnGui you did "Score : " + bestLevel instead of "Score : " + newScore.ToString() :)
     
  6. mdyerga

    mdyerga

    Joined:
    Aug 8, 2018
    Posts:
    4
    Having a similar problem keeping score. My score was just saying one for some reason I could not locate. I need it to count deaths. Currently I have...

    A script on a text game object...

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class keepScore : MonoBehaviour {
    public static int deathCounter;
    public Text deathText;


    // Use this for initialization
    void Start () {
    deathText.text = "Deaths:" + PlayerPrefs.GetInt("PlayerCurrentDeaths").ToString();
    }

    // Update is called once per frame
    void Update () {
    PlayerPrefs.GetInt("PlayerCurrentDeaths");
    }

    }

    and a script on an enemy...

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;


    public class killPlayer : MonoBehaviour
    {
    public GameObject objectToDestroy;
    int collisionCount;
    public static int score = 0;



    void Start()
    {

    }

    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.tag == "Player")
    {
    Debug.Log("You Died Sucker!!!!");
    SceneManager.LoadScene(SceneManager.GetSceneAt(0).name);
    score = score + 1;
    }

    if (other.tag == "Bullet")
    {
    Debug.Log("Enemy Died!!!!");
    collisionCount += 1;
    if (collisionCount > 16)
    {
    Destroy(objectToDestroy);
    }
    }

    }
    }

    Not sure what to add to cause my text game object to display a score number of times my player has died. I have been through several tutorials but none work. They were all from 2014. There may be a new way this has to be done.