- Home /
"If there is no save data" or "If there are no PlayerPrefs"
Is there anyway to code such a thing? I don't want to do an inefficient way of something like:
if(!PlayerPrefs.GetString("Transition Slide") && !PlayerPrefs.GetString("Armor") && !PlayerPrefs.GetString("Item") && !PlayerPrefs.GetString("Weapon") && !PlayerPrefs.GetInt("Health") && !PlayerPrefs.GetInt("Money")){
..as this is just way too much and there has to be something better.
Answer by Lukas H · Jun 21, 2013 at 08:18 AM
The official way of checking is:
PlayerPrefs.HasKey(KEY);
A for loop would also speed things up:
string[] keys = new string[]{"key1","key2", etc};
for(int i = 0, i < keys.length, i++)
{
//Do something usefull with this info
PlayerPrefs.HasKey(keys[i]);
}
That looks good. I think he just wants to check if ANY exist so my suggestion was to check one that only would exist if all others existed, no need to loop through
Answer by Slobdell · Jun 21, 2013 at 01:39 AM
I'm not sure I understand the question but if you're just checking if there's any previous playerprefs why don't you check one playerprefs and see if it exists, if not none do. Or set one like a bool for playerPrefsExist or something. You don't have to check every one...