• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rebekah-D-David · Jun 05, 2020 at 05:18 PM · booleansave datasaveloadtoggle buttoncheckbox

How To Save and Automatically Load each Toggle CheckBox Value ?

How can we store each of the checkbox value, that the next time we open the scene it can be automatically loaded ? Here's the code I have now:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class SettingsController : MonoBehaviour
 {
     bool[] settings;
 
     public Toggle[] toggles;
 
     void Start()
     {
         settings = new bool[toggles.Length];
         for (int i = 0; i < toggles.Length; i++)
         {
             settings[i] = true;
             int index = i;
 
             Toggle t = toggles[i];
             t.onValueChanged.AddListener(
 
                 (bool check) =>
                 {
                     CheckBox(index, check);
                 }
                 );
         }
     }
 
     public void CheckBox(int index, bool check)
     {
         settings[index] = check;
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SamuelRoos · Jun 07, 2020 at 03:28 AM

You could use Unity's PlayerPrefs for saving the toggle states. Since there is no method for saving booleans, you'd need to use an integer value as a bool (0 as false and 1 as true).

You can save a state of an individual toggle to the PlayerPrefs like so:


 //Let us imagine that up here is a *for loop* looping through all the toggles.
 
 //isOn returns the current state of a toggle as a bool
 //we convert this boolean to an integer using a *ternary conditional operator*
 //it's basically an if statement but within one line.
 int currentToggleState = toggles[i].isOn == true ? 1 : 0;
 
 //SetInt takes in two parameters; the key as a string + the actual value which is in this case an integer.
 PlayerPrefs.SetInt("NameOfThisParticularToggle", currentToggleState);
 
 //Boom, the value is now saved in the PlayerPrefs and will remain there forever until destruction
 //PlayerPrefs.DeleteAll() to clear them all, though be careful using this command



Now, if you wanted to load the value from the PlayerPrefs upon loading a scene, this is how you'd deal with that:


 //Again, here would be a for loop... too lazy to write it :)
 
 //Converting the integer back to a boolean using the conditional operator.
 toggles[i].isOn = PlayerPrefs.GetInt("NameOfThisParticularToggle") == 1 ? true : false



And here you go, this is how you'd do it.

Also, if you're struggling how to get the right PlayerPrefs value to a corresponding toggle when using a for loop (for example getting a saved int named as "GraphicalQuality" to the toggle which controls the quality), you could simply name all the toggle gameobjects differently, then save them to the PlayerPrefs by their gameobject names and then load the PlayerPrefs value which is named exactly like the current toggle, like so;


 //saving the value
 PlayerPrefs.SetInt(toggles[i].gameObject.name, toggles[i].isOn == true ? 1 : 0);
 
 //and loading it...
 bool isOn = PlayerPrefs.GetInt(toggles[i].gameObject.name) == 1 ? true : false



Also also, you will most likely get a error first time you load up the game as you haven't yet saved any player prefs though you are still trying to get them. You could do some checking based on if there is actually anything in the playerprefs, and if not, create them first.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rebekah-D-David · Jun 07, 2020 at 02:52 PM 0
Share

Thank you but sadly it dooesn't work

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save Game format that is change proof and secure 0 Answers

GUI Tickbox (Boolean) 1 Answer

Save And load Level Data in a game with one game scene and multiple levels saved as prefabs 1 Answer

Unable to save and load the game data on android 1 Answer

Saving progress even when I quit the windows app 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges