Help with PlayerPrefs

Hi,
I need some help with PlayerPrefs pleeeaseee… :frowning:

My camera have two scripts, I need a way to activate one and desactivate the other from my option’s menu (I has 2 GUI.Toggle) and that selection stores like a player preference… Any Idea??

Thanks… :?

On the Camera scripts, in the Start() function add something like…

if (the variable for this camera is false) {
gameObject.active = false;
}

So the GameObject of the camera will de-activate itself if your variable is not true, or stay on if it is true.

the problem with using 2 toggles in this case is that you cannot guarantee that 1 or the other is on, unless you check for that specifically in your game options code, so it could be that BOTH cameras get turned off because neither toggle was set to true.

Thanks you gave me a good hint to continue, but I have one more question, do you know in what script I have to store the values for playerPrefs?? and how??

:wink:

The easiest way is to initialise them in your “game manger” script that is a singleton and stays available for access from any other script very easily.

http://forum.unity3d.com/viewtopic.php?t=6734&highlight=singleton

Cheers :slight_smile:

One reason why this is a very good idea is that it prevents the player from editing their prefs on the fly after they’ve played the game once or even during gameplay.

We discovered that on Windows, the player prefs are read from the registry every time they are accessed, rather than cached inside Unity.

That lead to some very unpleasant exploits when players figured out they could edit the registry in real time to change how the game worked.

So reading in once and then accessing your in-game version is not only convenient, it prevents this sort of confusion and uncertainty.

Thanks for the advice!!! You are great…

Sorry, but I’m new in this, I did a game manager but I don’t know how playerPrefs works, how to store the selection of the gui.toggle inside a playerPrefs considering that is a bool(true or false)…

:sweat_smile:

Store your boolean value in prefs as a string, then read back in as a a string and reset your boolean based on checking the string content. The boolean casts to a string as True and False (uppercase), so use that when you check after you read it in.

// save boolean
showTutorials = false;
PlayerPrefs.SetString("Show Tutorials", ""+showTutorials);
// get it back
temp = PlayerPrefs.GetString("Show Tutorials", "True");
	if (temp == "True") {
	showTutorials = true;
	} else {
	showTutorials = false;
	}

Thanks… I put the code in my game manager script, but it have to be inside a special function or not?? :?

Those are just fragments – you have to use them where you were setting and getting the player prefs. And of course the variable names are just for example.

The default value in the GetPrefs call is used in case the key was never set before. So you use whatever state you want to be the default state there.

I know that I 'm doing something wrong, but I dont know what. And I get so confused!!! Pleasee help…

This is my script that is attached to a Gui.Text in a scene call options:

static var moveSlow = false;
static var moveFast = true;


function OnGUI () {
	
moveSlow = GUI.Toggle (Rect ((Screen.width/2)-60,(Screen.height/2)-100,150,15), moveSlow, "Move Slow");

PlayerPrefs.SetString("Move Slow", "moveSlow"); 

     
 
moveFast = GUI.Toggle (Rect ((Screen.width/2)-60,(Screen.height/2)-70,150,15), moveFast, "Move Fast");
 PlayerPrefs.SetString("Move Fast", "moveFast");

}

And this is the code that I have in a game manager script that is in the first scene.

function Pref () {
	temp = PlayerPrefs.GetString("Move Slow", "True"); 
	   if (temp == "True") { 
   moveSlow = true;
     } else { 
   moveSlow = false;
    
   }
   temp = PlayerPrefs.GetString("Move Fast", "False"); 
   if (temp == "False") { 
   moveFast = false; 
   } else { 
   moveFast = true; 
 
   } 

	}

The problem is that the changes that I made, didn’t store for the next game session… What I have to do to all the changes keeps store in the player preferences???

Thanks… :?: :?: :?: