Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Beebaa · Jan 12, 2021 at 01:51 PM · playerprefsbooleansavingtogglesaveload

How to save the state of Canvases/Toggles?

First of all, I don't really have any experience in coding and just started a while ago with Unity. Anyway, I almost got my first little application for an android device done, with a lot of help from various tutorials. There is still one major part missing though and that is saving the settings. I've tried almost every possible solution/asset I could find on the internet/asset store, but I didn't manage to get them to work or didn't figure out how they work yet.


This is what I want to do here: Firstly, I want to choose between different themes by clicking a toggle. Each toggle opens a canvas with a theme and closes every other theme canvas. With my current "saving" code, the toggle doesn't even activate the canvas I want to setActive (Without it does). Secondly, I want to save a GameObject as a favorite with a toggle and this toggle should still be toggled in the next session. Because the toggle always switched back to untoggled as soon as I clicked somewhere else, I made 2 overlapping toggles and each of them sets itself inactive and the other one active (I mention this if that is a reason why it doesn't work and I know it is probably not the professional way to do it).


I'm sorry for this beginner question, but I really couldn't figure it out on my own until now. I would really appreciate any kind of suggestions and thanks a lot in advance!

I've read PlayerPrefs is not the most secure way to save game data, but in this case, it doesn't matter. This is the currently not working saving code for the themes:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class SaveTheme : MonoBehaviour
 {
     public Canvas canvas;
 
     void Start()
     {
         if ((PlayerPrefs.GetInt("ThemeKey") == 1))
         {
             canvas.enabled = true;
         }
         else
         {
             canvas.enabled = false;
         }
     }
     
     void Update()
     {
         if (canvas.enabled == true)
         {
             PlayerPrefs.SetInt("ThemeKey", 1);
         }
         else
         {
             PlayerPrefs.SetInt("ThemeKey", 0);
         }
     }
 }

and this one for the favorite toggles:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class SaveToggle : MonoBehaviour
 {
     public Toggle toggle;
 
     void Start()
     {
         if ((PlayerPrefs.GetInt("ToggleKey") == 1))
         {
             toggle.isOn = true;
         }
         else
         {
             toggle.isOn = false;
 
         }
     }
 
     public void ToggleState()
     {
         if (toggle.isOn == true)
         {
             PlayerPrefs.SetInt("ToggleKey", 1);
         }
         else
         {
             PlayerPrefs.SetInt("ToggleKey", 0);
         }
     }
 }



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
Best Answer

Answer by Beebaa · Feb 09, 2021 at 07:48 AM

This is how I solved it now for the themes:

 private int value;
 public Canvas canvas;
 public Canvas canvas1;
 
 void Awake()
     {
         if (PlayerPrefs.HasKey("theme"))
         {
             value = PlayerPrefs.GetInt("theme");
 
             if(value == 0)
             {
                 canvas.enabled = true;
                 canvas1.enabled = false;
             }
             if (value == 1)
             {
                 canvas.enabled = false;
                 canvas1.enabled = true;
              }
              else
              {
                 canvas.enabled = true;
              }
     }
 
 public void OnClickSave()
     {
         if(canvas.enabled == true)
         {
             PlayerPrefs.SetInt("theme", 0);
         }
         else if (canvas1.enabled == true)
         {
             PlayerPrefs.SetInt("theme", 1);
         }
         PlayerPrefs.Save();
     }


And for the toggles:

 private int value;
 public Toggle toggle;
 public Toggle toggle1;
 
 void Awake()
       {
  
          if (PlayerPrefs.HasKey("toggle"))
          {
              value = PlayerPrefs.GetInt("toggle");
              if(value == 1)
              {
                  toggle.isOn = true;
              }
          }
  
          if (PlayerPrefs.HasKey("toggle1"))
          {
              value = PlayerPrefs.GetInt("toggle1");
              if (value == 1)
              {
                  toggle1.isOn = true;
              }
          }
      }
  
  public void OnFavoriteSave()
      {
          if (toggle.isOn == true)
          {
              PlayerPrefs.SetInt("toggle", 1);
          }
          if (toggle1.isOn == true)
          {
              PlayerPrefs.SetInt("toggle1", 1);
          }
          PlayerPrefs.Save();
      }
  
  public void OnUnFavoriteSave()
      {
          if (toggle.isOn == false)
          {
              PlayerPrefs.SetInt("toggle", 0);
          }
          if (toggle1.isOn == false)
          {
              PlayerPrefs.SetInt("toggle1", 0);
          }
          PlayerPrefs.Save();
      }

Comment
Add comment · 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

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

164 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 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

PlayerPrefs Not Saving When I Click the Button. 1 Answer

How do I apply and save my game settings? 0 Answers

how do i keep object destroyed after reloding scene ?? 0 Answers

PlayerPrefs Autosave [SOLVED] 2 Answers

Saving doesn't work after Build 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