Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by neo187-2 · Mar 23, 2012 at 11:13 AM · webplayerplayerprefssaveloading

Loading error when using PlayerPrefs.GetInt with WebPlayer

Hello

Something really odd happens with my game in the version distributed for webplayer (this does not happen in the PC version).

The game starts with a menu, then pressing New Game fires a function which uses PlayerPrefs.getInt to see if the player has already played the game and a "currentLevel" value has been stored in their PlayerPrefs.

Now, whenever I clear my webprefs, simulating a user with no prefs saved then the game only loads part of the scene, part of the textures, basically a broken scene with only part of what should be there. It's as if it starts loading the scene but then something breaks the process (an exception?).

However, after loading the scene my game automatically saves the currentLevel value with playerPrefs.setInt, thus creating a playerpref file on the machine. The following time the game is played everything is right.

Now what's happening here? I tried catching any execptions that may happen when the game is trying to retrieve the player pref like so:

 var prefs : int;
 
 //deal with any errors
 try{
     prefs = PlayerPrefs.GetInt("currentLevel");
 } catch (err){
     prefs = 0;
 }

Does this ring a bell for anyone? Thanks in advance for any help!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dakwamine · Mar 23, 2012 at 11:48 AM 0
Share

Concerning the syntax, you can do something simpler :

 var prefs : int;
 
 prefs = PlayerPrefs.GetInt("currentLevel");
 
 if(prefs != 0)
 {
     // currentLevel was already set OR == 0
 }
 else
 {
     // currentLevel was unset
 }

There is a second optional parameter in the PlayerPrefs.GetInt() that lets you decide the default value.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by neo187-2 · Mar 23, 2012 at 03:34 PM

Thank you Dakwamine, I did consider using the getInt() version with the defaut value. But then I came across this solution which has solved the problem fortunately:

 //deal with PlayerPrefs not existing on machine
 if(PlayerPrefs.HasKey("SaveGame")){
     prefs = PlayerPrefs.GetInt("SaveGame");
 } else {
     prefs = 0;
 }

The error is no longer happening, with or without the URL Prefs on the machine!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dakwamine · Mar 23, 2012 at 11:25 PM 0
Share

Ok, these are good news! :) If your question is answered, please accept your answer as the correct one (button on the left side of your answer). ^^ Goodbye!

avatar image rutter · Mar 24, 2012 at 01:02 AM 1
Share

Unless you want to have some special handling when the key isn't present, it can be much simpler to just pass a default value:

 //get the saved value, or default to zero
 prefs = PlayerPrefs.GetInt("SaveGame", 0);
avatar image
1

Answer by aldonaletto · Mar 23, 2012 at 12:52 PM

Could it be the 1M limit for Web Player prefs? PlayerPrefs are not size limited in stand alone versions, but have a 1M limit per Web player URL.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving boolean values using PlayerPrefs 1 Answer

PlayerPrefs.GetInt won't load when awake. 0 Answers

PlayerPrefs Question 0 Answers

Loading a save/playerprefs from a different game? 2 Answers

[PlayerPrefs] Shared saves between WebPlayer and Standalone? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges