Saving And Loading A Float

Hello

Im trying to save and load my exp heres my script

var exp : float;

function Update () {
    AddjustExp(0);
}

function AddjustExp(adj) {
    exp += adj;
}

how can i make it so if i press a gui button in the top middle of the screen it will save my exp and when i load another level it loads my exp.

Thanks

2 Answers

2
// Make this game object and all its transform children
// survive when loading a new scene.
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}

Right, for manager scripts that should survive the whole game that's the right way.

If you want to save the value even across multiple game sessions use PlayerPrefs.

To save your “exp”:

PlayerPrefs.SetFloat("currentXP",exp);

To load it:

exp = PlayerPrefs.GetFloat("currentXP",0);

The "0" at the end is the default value that is returned if it hasn't been saved yet. You can use any name you like to save values but it should be something you can remember ;)