maybe someone can shed some light on this particular situation (C#)
I have a Toggle for which I want to save state to playerprefs using bool.
Here is code doing that:
public void toggle()
{
if (toggleB.isOn == true)
{
SWDN = true;
}
else if (toggleB.isOn == false)
{
SWDN = false;
}
PlayerPrefs.SetInt("Speed", SWDN ? 1:0);
PlayerPrefs.Save ();
}
All is fine and dandy while game is runing. True / false states working normally and updating in “Regedit” as it should!
The moment I stop the game PlayerPrefs reset the bool to true even I set it to false and in “Regedit” it shows true when stopped!
This happen only with this bool, other string, int working just fine!
I had a similar issue, worked out I was setting the player stats in start which overwrote them. I used an if statement to pull the values back & set them to the int/float then check them, if they were null it was a new game & I used the new game values to set them for everything; if they weren’t null then I used those values.
So when game is running toggling the toggle set swdn to 0 or 1. So speed pref is working as well like that. Speed is either 0 or 1 just like swdn. The moment i stop game speed always is 1 in prefs even if before stop is set to 0. Why? It just always reset to 1 on game stop, no matter that i set it to 0 before stop. i dont have problem on calling the value upon start it looks like problem is in pref stay in state i set before game stop.
where do you get the playerprefs? is that in another script? so far yo uare setting them & saving them but it doesn’t look like you get them anywhere. It also doesn’t look like you are saving the player prefs anywhere except for when you call SetPlayerName.
Like i said prefs are not saved correctly! Problem is saving the pref! I see what is saved in regedit on windows and is always saved as true even if setted to false during game play! The moment i stop game that “Speed” always reset to true (1)!
I’m not sure what’s causing the problem, I’ve never had this issue but I set the playerpref for each instance inside each if statement so i explicitly set its value.
The reason everyone’s confused and asking for more information is that the problem can’t possibly be occurring here in the script you’ve copied- at least not because of anything you’ve done here. I’ll post the following list of suggestions, then leave you all to it.
You need to put “Debug.Log(“TOGGLE TRIGGERED!:” + toggleB.isOn.ToString());” right before or after the SetInt and figure out if this is where the problem is occurring.
You need to put “PlayerPrefs.Save();” back to where it was, after the SetInt. If removing it didn’t help, then not having it there is only going to further confuse the issue.
If the new Debug.Log in the toggle is showing “FALSE” as the last entry in your log after a new test, you need to do a search for other scripts that are calling “PlayerPrefs.SetInt(“Speed””, because it’s being overwritten somewhere else.
Opposite of the above case, if Debug.Log is showing “TRUE” as the last entry in the log, and you’re not intending to toggle it to true, you need to find out where the “toggle()” function can be called from (everywhere it can be called from) and put Debug.Logs around to find out what’s calling it and why.
Hi Lysander, thanks for your time. Did as you said and ui.toggle is always reseting back to true state on game stop which makes SWDN true again. So question should be how to prevent ui.toggle going back to true on exit/stop game?