Author Topic: UILabel with PlayerPrefs  (Read 3902 times)

Cybershead

  • Guest
UILabel with PlayerPrefs
« on: October 24, 2013, 03:39:01 PM »
Heya,

I know this probably a noobie question, so i will say sorry now but i cannot find the answer i need.
I just want to save a score in the player prefs. I will need a Grid but first i want to get past this. would anyone be able to help please, as you see the below code will show a score but the commented out code is wrong. Hope you can help

Cybershead




using UnityEngine;
using System.Collections;

public class HighScores : MonoBehaviour {
   
   
   public UILabel highScore;

   
   void Start () {
   //     highScore = PlayerPrefs.GetInt("HighScorePref");
   }

   void Update () {
      
   highScore.text = ScoresManager.CurrentPoints.ToString ();
   //   highScore = PlayerPrefs.SetInt("HighScorePref");
      
   }
}

missingno

  • Guest
Re: UILabel with PlayerPrefs
« Reply #1 on: October 24, 2013, 03:55:55 PM »
This is assuming that ScoresManager.CurrentPoints is an int. Also setting it in Start is useless since it will be immediately overwritten in the Update.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class HighScores : MonoBehaviour
  5. {
  6.         public UILabel highScore = null;
  7.        
  8.         void Update ()
  9.         {
  10.                 highScore.text = ScoresManager.CurrentPoints.ToString();
  11.                 PlayerPrefs.SetInt("HighScorePref", ScoresManager.CurrentPoints);
  12.         }
  13. }

Cybershead

  • Guest
Re: UILabel with PlayerPrefs
« Reply #2 on: October 24, 2013, 04:24:38 PM »
Thank you missingno

Quick response, well i think i have bigger problems as its a static int, but at least i know now how to save a UILabel. guess i should re look at this points system.

Have a great Day/Night :)

Cybershead

Cwal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: UILabel with PlayerPrefs
« Reply #3 on: October 24, 2013, 04:44:52 PM »
Why do you want it to be a static int?  You can just pull from the player prefs once, then when the highscore is beaten, set it to the current score and repeat.

missingno

  • Guest
Re: UILabel with PlayerPrefs
« Reply #4 on: October 25, 2013, 08:35:25 AM »
Thank you missingno

Quick response, well i think i have bigger problems as its a static int, but at least i know now how to save a UILabel. guess i should re look at this points system.

Have a great Day/Night :)

Cybershead

static int is fine, all static means is that there will only be a single instance of CurrentPoints no matter how many instances of ScoresManager there are (all instances of ScoresManager will reference the same CurrentPoints).