[SOLVED] PlayerPrefs bool problems in reseteing the saved bool

Hi,

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!

Any thoughts on this?

Alen

PlayerPrefs can’t interact with booleans directly, so I don’t see how that would be changing the state.

How do you initialize this SWDN bool at game start? The error must be there.

2 Likes

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.

1 Like

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.

Can you show us the top of the script where you declare the variables & your start function.

Like i said problem is keeping PlayerPref “Speed” value (0) when i stop the game!

    public bool SWDN;
    public Toggle toggleB;
    public GameObject ToggleBtn;
    public float qualityLevelChk;
    
    void Awake()
    {
    DontDestroyOnLoad(ToggleBtn);
    }

    void Start()
    {
               ChkQL ();
    }

    public void toggle()
    {
        if (toggleB.isOn == true)
        {
            SWDN = true;
        }
        else if (toggleB.isOn == false)
        {
            SWDN = false;
        }
       PlayerPrefs.SetInt("Speed", SWDN ? 1:0);
    }


    public void SetPlayerName(string value)
    {
        name = value;
        PlayerPrefs.SetString ("Player Name", name);
        PlayerPrefs.Save ();
    }

    public void ChkQL()
    {
        qualityLevelChk = GameObject.Find ("Menu").GetComponent<UI> ().qualityLevel;
    }

    }
}

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)!

because it looks like you set the player pref but don’t save it. Try saving the prefs after you set the speed one as well.

Putting PlayerPrefs.save (); gives same behaviour, thanks

Where are you putting it tho? From your code sample you only have it when the player sets their name.

On the first post you can see that it was there and it produced same behaviour :smiling_face:

Sorry, it’s been a long day :sweat_smile:

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.

It is ok, thanks for your time will try again with …i don’t know…skipping this part for now

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.
2 Likes

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?

Basically what I have to do i set separate function on Toggle…so now it has two one for SWDN and one for pref save
Solved:

    public string name;
    public bool SWDN;
    public bool vibro;
    public Toggle toggleB;
    public Toggle Vibrate;
    public GameObject ToggleBtn;
    public float qualityLevelChk;

    public bool SpeedPref;
    public bool VibroPref;

    void Awake()
    {
        name = PlayerPrefs.GetString ("Player Name");

        if (PlayerPrefs.HasKey ("PrefSpeed"))
        {
            toggleB.isOn = PlayerPrefs.GetInt ("PrefSpeed") == 1 ? true : false;;
        }
        else
        {
            toggleB.isOn = true;
        }

        if (PlayerPrefs.HasKey ("PrefVibro"))
        {
            Vibrate.isOn = PlayerPrefs.GetInt ("PrefVibro") == 1 ? true : false;;
        }
        else
        {
            Vibrate.isOn = true;
        }

    DontDestroyOnLoad(ToggleBtn);
    }

    void Start()
    {
        ChkQL ();
    }

    public void toggle()
    {
        if (toggleB.isOn)
        {
            SWDN = true;
        }
        else
        {
            SWDN = false;
        }
    }


    public void SetPlayerName(string value)
    {
        name = value;
        PlayerPrefs.SetString ("Player Name", name);
        PlayerPrefs.Save ();
    }

    public void ChkQL()
    {
        qualityLevelChk = GameObject.Find ("Menu").GetComponent<UI> ().qualityLevel;
    }

    public void VibrateBool()
    {
        if (Vibrate.isOn)
        {
            vibro = true;
        }
        else
        {
            vibro = false;
        }

    }

    public void SavePrefSpeed()
    {
        PlayerPrefs.SetInt("PrefSpeed", toggleB.isOn ? 1:0);
        PlayerPrefs.Save ();
       // Debug.Log ("TRIGGER Speed: " + toggleB.isOn.ToString());
    }

    public void SavePrefVibrate()
    {
        PlayerPrefs.SetInt("PrefVibro", Vibrate.isOn ? 1:0);
        PlayerPrefs.Save ();
      // Debug.Log ("TRIGGER Vibro: " + Vibrate.isOn.ToString());
    }

Thanks all!