PlayerPrefs issue with my code.

I started to mess with PlayerPrefs and from what have read on unity scripting guide this is what I came up with. The script below in attaced to my main camera in the char creation scene.

using UnityEngine;
using System.Collections;

public class HudDisplay : MonoBehaviour 

	{


	public float myFloat = 0.0f;
	public GUIStyle myGUIStyle;


	// Use this for initialization
	void Start () 
	{
		myFloat = PlayerPrefs.GetFloat ("lanistaGold");
	
	}
	



	void OnGUI() 
	{

		GUI.Label (new Rect (50, 0, 50, 25), myFloat.ToString(), myGUIStyle);

	}
}

This script is attachecd to my game manager object in the gameplay scene.

using UnityEngine;
using System.Collections;

public class CharCreation : MonoBehaviour 
	{

	public string lanistaName = "";
	public string lanistaAge = "";
	public GUIStyle myGUIStyle;
	public float lanistaPoints = 10;
	public float charismaPoints = 15;
	public float favorOfGodsPoints = 15;
	public float lanistaGold = 1000;

     

	void OnGUI() 
	{


		
		//...............................................................................................................................................


		GUI.Label(new Rect (275, 420, 200, 50), "Gold",  myGUIStyle);
		GUI.Label(new Rect (485, 420, 50, 50), lanistaGold.ToString(),  myGUIStyle);

		if (GUI.Button (new Rect (550, 420, 100, 50), "+")) 
		{
			if (lanistaPoints >= 1) 
			{
				lanistaGold += 100f;
				lanistaPoints -= 1f;
				PlayerPrefs.SetFloat("lanistaGold", lanistaGold);
			}
		}


		if (GUI.Button (new Rect (675, 420, 100, 50),"-")) 
		{
			if(lanistaGold >=1100) 
			{
				lanistaGold -= 100f;
				lanistaPoints  += 1f;
				PlayerPrefs.SetFloat("lanistaGold", lanistaGold);
			}
		}

		//.................................................................................................................................................







	}




}

This Script is what the PlayerPrefs is getting its info from.

Now my issue is when I change the value of lanistagold in my creation screen and load up the game play level the number does display like I want it. But If I reload the game and don’t change the value with the button then when I load the game level its same as before.

For example:

I use 1 lanista point to give me 100 more gold so then I get 1100 gold. I load level and it displays 1100. I restart game.

I use no points and leave it at the default value and load level. It still displays 1100.

Further Testing It seems I have to hit either the + or - button for the script to update. even if I close the game and restart it the script is still saving the last amount of gold that was set.

You know PlayerPrefs is like a savestate, it saves the last valve you set to it. To clear you need to reset the valve to your default one via PlayerPerfs. Alternative you can mess around with alternative variables :slight_smile: I didn’t read carefully your code, yet but do you load it at the beginning of the level ? You can clear it there or just remove the code line :slight_smile: