The question is answered, right answer was accepted
How can I update UI Text score using PlayerPrefs.Getint() on startup?
Hello I've managed to get my PlayerPrefs.GetInt(); To successfully transfer to my 2nd scene, but what appears to be the problem on the 2nd level is that the score would be set default to 0 and would only got back to it's playerpref int once another scorepoint is picked up. I'm not sure what I'm doing wrong or what I have missed.(Written in C#)
public Text CountText;
public AudioClip Pickup;
private int count ;
// Use this for initialization
void Start ()
{
count = 0;
PlayerPrefs.SetInt ("count", count);
PlayerPrefs.Save ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{ //anything with the tag "pick me up" disappers on contact with the player
if (other.gameObject.CompareTag ("PICKMEUP")) {
other.gameObject.SetActive (false);
count = PlayerPrefs.GetInt("count") + 1;
SetCountText();
float volume = 0.2f;
AudioSource.PlayClipAtPoint(Pickup, transform.position, volume);
PlayerPrefs.SetInt("count", count);
PlayerPrefs.Save();
}
}
void SetCountText ()
{
CountText.text = "X " + count.ToString ();
}
On my second level I have a script that is almost a duplicate to the one above, but with slight changes.
void Start () {
if(PlayerPrefs.HasKey("Test"))
{
count = PlayerPrefs.GetInt("count");
SetCountText ();
}
}
After this second image I would pick up a coin and get the correct Int, but how can I fix it not displaying the right Int at the start of the second level?
Answer by liam6478 · Nov 27, 2015 at 01:39 AM
I have realised my mistake, my apologiies.
if(PlayerPrefs.HasKey("Test"))
{
count = PlayerPrefs.GetInt("count");
SetCountText ();
}
Should have been:
if(PlayerPrefs.HasKey("count"))
{
count = PlayerPrefs.GetInt("count");
SetCountText ();
}
Follow this Question
Related Questions
C# Player Prefs & Toggle| Will not save settings 1 Answer
How to change GUI label size 1 Answer
Update and Get PlayerPrefs during runtime? 0 Answers
How to do + or - functions to PlayerPrefs.SetInt? 1 Answer
Playerprefs value 1 Answer