Home > Blockchain >  How to tick and untick a toggle from script in unity (graphically)
How to tick and untick a toggle from script in unity (graphically)

Time:04-12

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

CodePudding user response:

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

toggle.isOn = true;

Reference for Toggle

CodePudding user response:

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

  • Related