Can you help me how to assign the initial data to the game. I used the PlayerPrefs.GetInt, SetInt class, in the game. But when calling it in start function or wake function, when game starts, it set the data again. Can you help me how to only call this function once when installing the game. Or how to assign data when installing the game. Thank you!
Asked
Active
Viewed 65 times
-1
-
give a look to [scriptableObjects](https://docs.unity3d.com/Manual/class-ScriptableObject.html) – rustyBucketBay Jan 05 '22 at 05:41
-
`GetInt(string key, int defaultValue);` You can use PlayerPrefs defalult value for initial – siusiulala Jan 05 '22 at 05:47
-
Whatever way you use to [save/load data](https://stackoverflow.com/questions/40078490/saving-loading-data-in-unity) simply somewhere set and store a check value (a simple bool, a certain number etc) -> before loading the data check the value -> skip loading if it is already set. However, this might be a XY problem ... what about simply already serialize and store this data into your app and when you save use an external file -> if external file exists => you use that external file and not the default data ... – derHugo Jan 05 '22 at 07:51
1 Answers
0
When game awake, load PlayerPrefs with default value and write back PlayerPrefs to make sure data initialized.
private void Awake()
{
int defaultValue = INT_VALUE;
int data = PlayerPrefs.GetInt("some_data", defaultValue );
PlayerPrefs.SetInt("some_data", data);
}

Takeshi
- 16
- 2
-
what would be the use of writing back the unchanged value directly? it will be `123` which is the default value anyway ... – derHugo Jan 05 '22 at 08:05