- Home /
player pref first time set up
hi im trying to set the player pref for first time a player uses the game. so i want to do
if(PlayerPrefs.GetInt("example") == null)
{
PlayerPrefs.SetInt("example",1)
}
is null the default setting on playerprefs? thanks
string isLoginOnce = PlayerPrefs.GetString ("_isLoginOnce");
if(isLoginOnce !="True")
{
PlayerPrefs.SetString ("_isLoginOnce","True");
PlayerPrefs.SetString("playerName",userName);
PlayerPrefs.SetString("playerPassword",userPassword);
}
if(isLoginOnce == "True")
{
print (" isLoginOnce "+isLoginOnce);
userName = PlayerPrefs.GetString("playerName");
userPassword = PlayerPrefs.GetString("playerPassword");
}
Answer by Mehul-Rughani · Jun 30, 2014 at 12:40 PM
PlayerPrefs.HasKey("example")
it will return true if it is assigned already otherwise it will return false..
so You Can Write
if(!PlayerPrefs.HasKey("example")) PlayerPrefs.SetInt("example",1);
Answer by DaveA · Nov 30, 2012 at 10:00 PM
Just use
PlayerPrefs.GetInt("example", 1);
getintexample? i found the default is 0 so i put .setint(example,1) thanks
@Holyheadit: The point is that GetInt allows you to set a default value. So you never need to check for whether anything is null. e.g.,
var foo = PlayerPrefs.GetInt("foo", 99);
This way foo is set to 99 if there is no player prefs set for "foo", otherwise it gets the value from what was saved. Then later you just do
PlayerPrefs.SetInt("foo", foo);
later on as usual. You don't need to use SetInt to set a default value, since that's already taken care of with GetInt.
i meamt on a first time run im looking for first time settings so if it wasby default 0 then if(getint=0) go settings