Issue with PlayerPrefs

I have two actually. One with PlayerPrefs.SetInt. And another with PlayerPrefs.SetString.

I have two scenes. In my first scene a variable is displayed; Economy, in my second scene this Economy var is altered. When I return to scene one after altering this var, it retains the change, however, when I return to the second scene, it resets itself back to 0. I understand I can use PlayerPrefs to save this, but I am not too sure exactly how to write them, or how to implement them across scripts. At the moment I am accessing the script via;

var script : scriptName;
script = GetComponent(“scriptName”);

Which works perfectly, but obviously doesn’t save the change.

My other issue is to do with the same scene. I have GUI buttons in my second scene, and when clicked they delete themselves, increasing a variable I have set; Build, by 1. Now I can leave this scene, but when I return to it, the GUI buttons have returned, and the Build var, has been set back to zero. I was wondering how I can save these GUI changes, and also my variable changes across the entire game.

I am using booleans, and setting them to false to make the GUI buttons disappear.

I know I am asking a lot, so any advice, no matter how small, is greatly appreciated. Even if you can only answer one, I really need it, guys. Thanks.

Well… you can use PlayerPrefs or you can create a script with DontDestroyOnLoad() so that will be consistent also when you’ll change scene.
I always do something like Options or PlayerStats or something that will never be destroyed and access it in all my scenes (sometimes using static methods).
Generally I use the PlayefPrefs just to permanently save the stats on disc.

To retain info across scenes, you’ll likely want to keep that info in some sort of a “Manager” script (likely a singleton). Normally, the Manager script would be associated with an Empty game object and would call “DontDestroyOnLoad” in Start or Awake. That’ll ensure that the game object the script is associated with will be retained across level loads (and so will the script itself).

With that in place, you can store any game-wide info in the manager script. It’ll be easy to access it from other scripts and the associated data will be retained for the entire run of the game.

So, that basic setup can be used to solve your running game instance data issues. Also, you can expand on the general idea as necessary. I tend to use a number of manager singleton objects (AudioManager, InputManager, GUIManager, GameManager, etc). However, if you want to persist any of that data for use by a later game instance (if you start up the game tomorrow), you’ll need to use something to store it to non-volatile memory. PlayerPrefs is an option here.