Greetings. It’s been a while when I had visited this forum
I’ve been struggling with this strange behaviour for two days and jumping between two AI to get it sorted out with no success.
Somehow I just can’t save float values with PlayerPrefs. Everytime when I check through Regedit.
There’s only a error on saved data: Invalid DWORD (32-bit) value with a garbage in value.
But! When I save int or string value it works flawlessly.
Would it be possible because i’m running originally Windows 11 23H2 english version and added
locales for finnish?
Read about using CultureInfo.InvariantCulture because some countries use period instead of comma in fractions. As we know; “Unity handles the conversion of the float value internally and ensures that it’s saved in a platform-independent manner. You don’t need to worry about formatting or culture-specific issues when saving floats to PlayerPrefs directly.” ← Sounds good on paper…
I was using Unity 2022.2.20f so I decided to update to 2022.3.19f if that helps.
Nope. Same behaviour continues.
I wouldn’t want to save certain floats from sliders to string and convert them somehow back to float for
actually use of those values.
Those values are being used in Options menu with sliders for adjusting brightness, music volume and fx volume.
Should I just forget using floats for more accurate results and set things to work with int?
I’ll post those three scripts what I used when debugging this problem:
First is for saving string:
using UnityEngine;
public class PlayerPrefsStringExample : MonoBehaviour
{
private const string stringTestKey = "StringTest";
void Start()
{
// Save the string "This is A test!" to PlayerPrefs
SaveStringToPlayerPrefs(stringTestKey, "This is A test!");
// Retrieve the saved string from PlayerPrefs
string retrievedString = RetrieveStringFromPlayerPrefs(stringTestKey);
// Print the retrieved string to the Unity console with a message
Debug.Log("Retrieved String: " + retrievedString);
}
// Function to save a string value to PlayerPrefs
private void SaveStringToPlayerPrefs(string key, string value)
{
PlayerPrefs.SetString(key, value);
PlayerPrefs.Save();
}
// Function to retrieve a string value from PlayerPrefs
private string RetrieveStringFromPlayerPrefs(string key)
{
return PlayerPrefs.GetString(key, ""); // Provide a default value ("") if the key doesn't exist
}
}
Second is for int:
using UnityEngine;
public class PlayerPrefsExample : MonoBehaviour
{
private const string devilsNumberKey = "DevilsNumber";
void Start()
{
// Save the integer value 666 to PlayerPrefs
SaveIntToPlayerPrefs(devilsNumberKey, 666);
// Retrieve the saved integer value from PlayerPrefs
int retrievedValue = RetrieveIntFromPlayerPrefs(devilsNumberKey);
// Print the retrieved integer value to the Unity console
Debug.Log("Devil's Number: " + retrievedValue);
}
// Function to save an integer value to PlayerPrefs
private void SaveIntToPlayerPrefs(string key, int value)
{
PlayerPrefs.SetInt(key, value);
PlayerPrefs.Save();
}
// Function to retrieve an integer value from PlayerPrefs
private int RetrieveIntFromPlayerPrefs(string key)
{
return PlayerPrefs.GetInt(key, 0); // Provide a default value (0) if the key doesn't exist
}
}
And third what stopped my progress with floats:
using UnityEngine;
public class PlayerPrefsFloatTest : MonoBehaviour
{
private const string floatKey = "MyFloatValue";
private float defaultValue = 0.69f;
void Start()
{
// Save a float value to PlayerPrefs
SaveFloatToPlayerPrefs(floatKey, defaultValue);
// Retrieve the saved float value from PlayerPrefs
float retrievedValue = RetrieveFloatFromPlayerPrefs(floatKey, defaultValue);
// Log the retrieved float value
Debug.Log("Retrieved Float Value: " + retrievedValue);
}
// Function to save a float value to PlayerPrefs
private void SaveFloatToPlayerPrefs(string key, float value)
{
PlayerPrefs.SetFloat(key, value);
PlayerPrefs.Save();
}
// Function to retrieve a float value from PlayerPrefs
private float RetrieveFloatFromPlayerPrefs(string key, float defaultValue)
{
return PlayerPrefs.GetFloat(key, defaultValue);
}
}
As you can see there aren’t much changes between them but i’ll post those scripts because why not