Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

Android, How can I permanently save data? (A couple variables) 4 Answers

Why does PlayerPrefs save to the Windows Registry? 0 Answers

PlayerPrefs.GetString not saving past values 0 Answers

Get Asset at runtime by its ID 0 Answers

PlayerPrefs always return 0 but health variable is 5,I have a problem PlayerPrefs. It always return zero when it starts. But I assign variable 5 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges