2
\$\begingroup\$

As the title says I would like to know if playerprefs can fail to save.

If they can, is there some way to check if it save properly / if it failed?


Example :

if (playerprefsFailed)
{
    Debug.Log("Failed to save playerprefs...");
}

I found PlayerprefsException in the Unity API, however I'm confused on how to use it.

I have also googled if they can fail, but couldn't find any results, however considering there is a exception for it, it can probably fail.

The PlayerprefsExceptionis also only thrown on the web player however I would like to know if it can fail, and when, and also how to stop it on a normal pc build.

\$\endgroup\$
7
  • \$\begingroup\$ Maybe the exception is for retrieving data? I couldn't find anything on failing or checking if it fails either. Sorry. \$\endgroup\$
    – Mike Hunt
    Mar 5, 2016 at 17:48
  • 1
    \$\begingroup\$ It seems that, at least currently, PlayerPrefsException is only thrown in Web Player builds when the preference file exceeds the allotted 1MB size: docs.unity3d.com/ScriptReference/PlayerPrefsException.html \$\endgroup\$
    – Joe
    Mar 5, 2016 at 17:55
  • \$\begingroup\$ I'd also suggest that in absence of documentation for it, we can assume that PlayerPrefs.Save() will throw an exception if something goes wrong, but I can't test that right now. \$\endgroup\$
    – Joe
    Mar 5, 2016 at 18:03
  • \$\begingroup\$ @Joe How would I go about testing it? \$\endgroup\$
    – BiiX
    Mar 5, 2016 at 18:13
  • \$\begingroup\$ My best idea on that is construct an environment where the default storage location of PlayerPrefs is unwritable, then try saving. Might not be so easy depending on platform. \$\endgroup\$
    – Joe
    Mar 5, 2016 at 19:04

1 Answer 1

4
\$\begingroup\$

PlayerprefsException will be usually be thrown in web app where 1mb limit gets passed. Docs

To check if playerpref got saved use,

if(PlayerPrefs.HasKey("myFloat"){
PlayerPref.setFloat("myFloat",myFloatValue);
}else{
//First time initlisation of playerPref
}


if( !PlayerPrefs.HasKey("myFloat") || PlayerPref.getFloat("myFLoat") != myFloatValue){
 Debug.Log("Failed to save playerprefs...");
}
\$\endgroup\$
4
  • \$\begingroup\$ Is it impossible for the PlayerPrefs to save on a non-web game? I mean, lets say the playerprefs file for some reason can't be written to, what will unity do? And how can I alert the player about this issue? \$\endgroup\$
    – BiiX
    Mar 6, 2016 at 16:58
  • \$\begingroup\$ On Windows standalone, there's no need to expect failure. If an application cannot write something into Registry, then something is very wrong with Windows itself. \$\endgroup\$ Mar 10, 2016 at 19:06
  • \$\begingroup\$ @BiiX correctly pointed by maxim ,that may be the reason why unity not implemented exception for it. \$\endgroup\$
    – idurvesh
    Mar 12, 2016 at 5:47
  • \$\begingroup\$ @MaximKamalov Ah, I see. \$\endgroup\$
    – BiiX
    Mar 12, 2016 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .