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??
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??
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.
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.
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)…
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;
}
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.
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???