Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

592
Views
Toggle bool with checkbox and playerprefs in Unity C#

I'm trying to make a checkbox in Unity UI that returns a bool endless game as 'true', and 'false' if it's a timed game. I want to do this with PlayerPrefs. I understand that PP only supports int, float and strings. I created a function that true to 1.

I have a Game Manager that handles the overall state of the game, this gameobject is the only one in my game that doesn't get destroyed on load.

So far the Game Manager has a bool 'endless' that is initialized as

endless = PlayerPrefs.GetInt("Endless", 1) == 0;

in the start function.

Now I have an options menu that houses the checkbox, in this script is the following:

public void OnToggleEndless()
{
    if (endLessToggle.isOn)
    {
        PlayerPrefs.SetInt("Endless", manager.endless ? 1 : 0);
        
        print("Endless Button ticked");
    }
    else 
    {
        PlayerPrefs.SetInt("Endless", manager.endless ? 0 : 0);
        print("Endless Button unticked");            
    }
    
}

But when I hook it up, play the game and go to the options menu and tick 'Endless Game', nothing happens. I tried to put the OnToggleEndless(); method in Update, but that just gives me endless lines of the print("Endless Button ticked"); message, the PlayerPrefs aren't saved. I'm wondering if the checkbox is even hooked up properly. I cannot figure this one out!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The PlayerPrefs aren't saved

The saving of the PlayerPrefs seems faulty are you sure you want to actually check for manager.endless instead of endLessToggle.isOn.

Because the state of endLessToggle.isOn changes depending if you enabled or disabled the checkbox. But the state of manager.endless never changes and is set to either true/false at the start of the game.

This results in you not actually changing the PlayerPrefs variable when you toggle the checkbox.

Example:

private void Start() {
    // Check if we enabled or disabled the endlessToggle Checkbox last time we played
    // and set the checkbox accordingly.
    endLessToggle.isOn = (PlayerPrefs.GetInt("Endless", 1) == 0);
}

// Call when we toggle the Checkbox.
public void OnToggleEndless() {
    // Get the current state of our toggle button.
    int enable = endLessToggle.isOn ? 1 : 0;
    // Log result into console to test it.
    Debug.Log("We set Endless to: " + enable);
    // Set the PlayerPrefs equal to our current state.
    PlayerPrefs.SetInt("Endless", enable);
}

I don't think you need to use a a global variable from your GameManager anyway, because you already have the PlayerPrefs which does the same thing for you.

Additionally you need to set the state of the CheckBox equal to the PlayerPrefs when you start the Game.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!