Hey, I am having troubles getting playerprefs to save and load a value (in this case I want it to save the highest score). I have a scoresystem in my Player.cs script.
using UnityEngine;
using System.Collections;
public class Lose : MonoBehaviour
{
private int buttonWidth = Screen.width / 2;
private int buttonHeight = 50;
void Start()
{
PlayerPrefs.SetInt("Player Score", Player.Score);
getHighScore();
}
void OnGUI ()
{
GUI.Box (new Rect (Screen.width / 2 - buttonWidth / 2, 0, buttonWidth, buttonHeight), "You lost");
if (GUI.Button (new Rect (Screen.width / 2 - buttonWidth / 2, 75, buttonWidth, buttonHeight), "try again"))
{
Application.LoadLevel (1);
}
GUI.Label (new Rect (Screen.width / 2 - buttonWidth / 2, 195, buttonWidth, buttonHeight), "Score " + Player.Score);
GUI.Label (new Rect (Screen.width / 2 - buttonWidth / 2, 205, buttonWidth, buttonHeight), "High Score " + PlayerPrefs.GetInt("Player Score"));
}
void getHighScore()
{
if (Player.Score > PlayerPrefs.GetInt("Player Score"))
PlayerPrefs.SetInt("Player Score", Player.Score);
}
}
When I build and run the game both score field are the same. Does anyone know a fix for this?