0

I'm creating a game in unity and I'm doing the settings page which is either on the main menu and while playing. I have a toggle for enabling vsync and disabling but if the check is checked in the main menu it's not checked while playing so I just created a bool and if it is true I want it to check the toggle and if it false to uncheck it but I don't know how to do it and I didn't find answers. Thank you very much

public void Modifyvsync()
{
   if (vsynconoff.isOn)
    {
        QualitySettings.vSyncCount = 1;
        
        
    }
   else
    {
        QualitySettings.vSyncCount = 0;
        

    }
    PlayerPrefs.SetInt("vsyncount", QualitySettings.vSyncCount);
}

this is a method that I linked to a toggle, but if I get to the play scene and I return back to the settings menu the toggle isn't more checked even if I checked the toggle. I want it to remain checked even after I change scene

BONG8
  • 3
  • 3

2 Answers2

1

You can set the value of the toggle component using the isOn property.

toggle.isOn = true;

Reference for Toggle

hijinxbassist
  • 3,667
  • 1
  • 18
  • 23
  • resolved using Player.Prefs and using a bool variable thanks to your answer and another post https://stackoverflow.com/questions/41073236/how-to-save-bool-to-playerprefs-unity. – BONG8 Apr 11 '22 at 14:07
0

resolved

public bool isvsyncactive;

int booltoint(bool val)
{
    if (val)
        return 1;
    else
        return 0;
}
bool intToBool(int val)
{
    if (val != 0)
        return true;
    else
        return false;
}

public void Modifyvsync()
{
   if (vsynconoff.isOn == true)
    {
        QualitySettings.vSyncCount = 1;
        isvsyncactive = true;

        
        
    }
   else
    {
        QualitySettings.vSyncCount = 0;
        isvsyncactive = false;
        

    }
    PlayerPrefs.SetInt("vsynconoroff", booltoint(isvsyncactive)) ;


void Update()
{    vsynconoff.isOn = intToBool(PlayerPrefs.GetInt("vsynconoroff"));
}

The thing is just that I take a bool variable taht is true if vsync is active and false if vsync is disabled and I convert it to 1 for true and 0 for false bacuse PlayerPrefs can't store bool variables inside. So then in the update method I just convert it to bool again and then if it's true the toggle will be ticked if I restart the scene

BONG8
  • 3
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 11 '22 at 16:14