0
\$\begingroup\$

Can I ask for help/guide where I can build 2d game on android device includes the players prefs that I defined already, for example I have an "int" PlayerPrefs (PlayerPrefs.Getint ("mysavenumber")),

the data "mysavenumber" has already a saved integer value in it. How ever upon building my game and installing it on my android device it seems does not hold any value.

How can I build my game with the Playerprefs data that I have defined upon building and installing it on an android device?

\$\endgroup\$
1
  • \$\begingroup\$ PlayerPrefs.GetInt allows you to specify a default value to return if the key is not present. Would that cover what you need? \$\endgroup\$
    – DMGregory
    Commented Jan 19, 2021 at 10:30

2 Answers 2

1
\$\begingroup\$

PlayerPrefs are stored locally on the device, they are not part of your app so they will not be set when you install it.

You have two options:

  1. Pass a default value to PlayerPrefs.GetInt(string name, int defaultValue), that will be the value that you want to set when you install. Unity will automatically use that value if you haven't called PlayerPrefs.SetInt() for that value yet.
  2. When you first run the game, go over all the values you wanted and set them to the pre-defined values that you want. This has the benefit of having all the default values in a single place instead of spread around the code whenever you call GetInt(). You can save another player pref called "default-set" and use it to make sure you only initialize the default values once, otherwise you will overwrite any changes that the player makes.
\$\endgroup\$
0
\$\begingroup\$

You can just create a game object and add a script to it and on the Start() method just call the PlayerPrefs and set the values you want. Now keep in mind that PlayerPrefs are only 1 megabyte of free local storage that you get. Thats why its called PlayerPrefs, its the preferences of the player, like sound settings, music settings, etc. Things that are usually small to store.

You can also just create a json file into the build with classes and the values you want to save and read and write to that json file the values you want. Or like the YouTuber Brackeys did with saving and loading game. Here is the video: https://www.youtube.com/watch?v=XOjd_qU2Ido

\$\endgroup\$

You must log in to answer this question.

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