PlayerPrefs saving and Loading

I got this little code. I made it so it’ll save the Floats and It’s from different values. Now, what am I missing? I’m very Newb’ish when it comes to PlayerPrefs.

using UnityEngine;
using System.Collections;

public class SaveState : MonoBehaviour {

	public AgeCounter ageC;
	public Click click;
	public EXP exp;
	public overallPoops poops;


	public void Save()
	{
		PlayerPrefs.SetInt ("Age", ageC.ageNum );
		PlayerPrefs.SetInt ("Age2", ageC.targetExp);
		PlayerPrefs.SetInt ("GPC", click.goldperclick);
		PlayerPrefs.SetInt ("exp", exp.experience);
		PlayerPrefs.SetInt ("expPS", exp.experiencePS);

		PlayerPrefs.SetFloat ("Gold", click.gold);
		PlayerPrefs.SetFloat ("OverallG", click.overalGold );
		PlayerPrefs.SetFloat ("GPC", click.goldperclick);
		PlayerPrefs.SetFloat ("Health", poops.health);
	}
	public void Load()
	{
		int Age = PlayerPrefs.GetInt ("Age", ageC.ageNum);
		int Age2 = PlayerPrefs.GetInt ("Age2", ageC.targetExp);
		int GPC = PlayerPrefs.GetInt ("GPC", click.goldperclick);
		int exp = PlayerPrefs.GetInt ("exp");
		int expPS = PlayerPrefs.GetInt ("expPS");

		float gold = PlayerPrefs.GetFloat ("Gold");
		float overallG = PlayerPrefs.GetFloat ("OverallG");
		float gpc = PlayerPrefs.GetFloat ("GPC");
		float health = PlayerPrefs.GetFloat ("Health");
	}

}

what did i Miss in Loading?

The Load function is storing data in local variables, once the Load function completes, all the data you retrieved is lost.

Change int Age = PlayerPrefs.GetInt ("Age", ageC.ageNum);

to ageC.ageNum = PlayerPrefs.GetInt ("Age", 0);

The “0” is the default value to use, if Age doesn’t exist in PlayerPrefs.


This is correct:

 using UnityEngine;
 using System.Collections;

public class SaveState : MonoBehaviour
{

    public AgeCounter ageC;
    public Click click;
    public EXP exp;
    public overallPoops poops;

    public void Save()
    {
        PlayerPrefs.SetInt("Age", ageC.ageNum);
        PlayerPrefs.SetInt("Age2", ageC.targetExp);
        PlayerPrefs.SetInt("GPC", click.goldperclick);
        PlayerPrefs.SetInt("exp", exp.experience);
        PlayerPrefs.SetInt("expPS", exp.experiencePS);

        PlayerPrefs.SetFloat("Gold", click.gold);
        PlayerPrefs.SetFloat("OverallG", click.overalGold);
        PlayerPrefs.SetFloat("GPC", click.goldperclick);
        PlayerPrefs.SetFloat("Health", poops.health);
    }

    public void Load()
    {
        int Age = PlayerPrefs.GetInt("Age");
        int Age2 = PlayerPrefs.GetInt("Age2");
        int GPC = PlayerPrefs.GetInt("GPC");
        int exp = PlayerPrefs.GetInt("exp");
        int expPS = PlayerPrefs.GetInt("expPS");

        float gold = PlayerPrefs.GetFloat("Gold");
        float overallG = PlayerPrefs.GetFloat("OverallG");
        float gpc = PlayerPrefs.GetFloat("GPC");
        float health = PlayerPrefs.GetFloat("Health");
    }

}

And indeed only use the second parameter if you want to set a default value.