Hey, I have been learning and working on data persistence through tutorials and Unity references, and I can’t get my Sensitivity.cs
script to save data to my (not created) json. So far based on what the tutorial has set, I should be able to save the data but not load it, and its not really saving anywhere in particular. The commented code at the bottom is what im trying to save, but none of the values that are in the code are accepted
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Sensitivity : MonoBehaviour
{
[SerializeField] protected CharacterMotorConfig Config;
[SerializeField] private TextMeshProUGUI ValueText;
[SerializeField] private Slider Slider;
private void Start()
{
Debug.Log($"Start ConfigIsSet={Config != null}");
var savedValue = PlayerPrefs.GetFloat("sensitivity", 30f);
Debug.Log($"Saved value is {savedValue}");
OnChangeSlider(savedValue);
Slider.value = savedValue;
}
public void OnChangeSlider(float Value)
{
// Value = 0 to 10
// produce a number between 0.1 and 0.5
var perc = Value / 50f; // calculate perc, because it produces a number between 0 and 10
var min = 0.1f; // the minimum value to use
var max = 50f; // the maximum value to use
var value = (perc * (max - min)) + min; // convert the percentage value to the min/max value
Debug.Log($"OnChangeSlider {Value} : {value}");
if (Config != null)
{
ValueText.SetText($"{Value:0}");
Config.Camera_HorizontalSensitivity = value;
Config.Camera_VerticalSensitivity = value;
PlayerPrefs.SetFloat("sensitivity", Value);
PlayerPrefs.Save();
}
}
/*
public void LoadData(GameData data)
{
this.Value = data.value;
}
public void SaveData(GameData data)
{
data.Sensitivity = this.savedValue;
}
*/
}
I am also having a problem with the the DataPersistenceManager
logging an error for another manager, which I believe could be Playerprefs
?
void Awake()
{
if (Instance == null)
{
Debug.LogError("Found more than one Data Persistence Manager in the scene.");
}
Instance = this;
}