- Home /
This question was
closed Feb 21 at 09:35 PM by
plexHD for the following reason:
Solved problem my self
Playerprefs resetting for no reason
I have a script in which I control my difficulty settings and the difficulty is stored in the player prefs. The switching is working perfectly fine but when I load another scene and then load the scene again, it suddenly resets to 0 again. Can somebody pls help me with that?
Here is the code:
public class Difficulty : MonoBehaviour { public Toggle[] Toggles; bool ToggleAllowed = true;
void Start()
{
for (int i = 0; i < Toggles.Length; i++)
{
if (i != PlayerPrefs.GetInt("difficulty"))
{
Toggles[i].isOn = false;
}
else
{
Toggles[i].isOn = true;
}
}
}
private void Update()
{
Debug.Log("Difficulty: " + PlayerPrefs.GetInt("difficulty"));
}
public void difficultySwitched(int difficulty)
{
if (ToggleAllowed == true)
{
PlayerPrefs.SetInt("difficulty", difficulty);
ToggleAllowed = false;
for (int i = 0; i < Toggles.Length; i++)
{
if (i != difficulty)
{
Toggles[i].isOn = false;
}
else
{
Toggles[i].isOn = true;
}
}
PlayerPrefs.Save();
Debug.Log(PlayerPrefs.GetInt("difficulty"));
Invoke("AllowToggle", 0.1f);
}
}
void AllowToggle()
{
ToggleAllowed = true;
}
}
Comment