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 /
avatar image
0
Question by Deinima · Apr 21, 2015 at 12:09 PM · playerprefsfreeze

Toggle UI Freezing Editor?

I'm having trouble with PlayerPrefs.GetInt() and PlayerPrefs.SetInt() in Unity 4.6.4f1. I have tried several versions of the code I'm trying to achieve, but nothing is working. I only have one script in my project at the time, with functions for UI toggles and buttons and such. The problem code, I have determined, is my use of PlayerPrefs. Whenever I comment the instances out and press play, it runs like normal. But restore them and it freezes the entire editor on play to where I must end the task in the task manager. The full script follows. If there is any other information that may be useful, please ask.

:: EDIT :: I did some more testing and I do not believe that PlayerPrefs are responsible after all. After -creatio- suggested I use Debug.Log, which never had a chance to run, I decided to instead try something else. I commented out all of my PlayerPref lines and tried manually setting the toggle settings. I found that... tog_music.isOn = true; ... Is what seems to be crashing my editor. Is this not the right way to do it? I should mention that this is the UnityEngine.UI Toggle option, not the OnGUI().

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ui_mainmenu : MonoBehaviour {
     public GameObject optionsPanel;
     public bool options;
 
     public Toggle tog_music;
     public Toggle tog_sound;
 
     void Start () {
         optionsPanel.SetActive(false);
         options = false;
         tog_music.isOn = (PlayerPrefs.GetInt("tog_music") == 1 ? true : false);
         tog_sound.isOn = (PlayerPrefs.GetInt("tog_sound") == 1 ? true : false);
     }
     
     void Update () {
         if (Input.GetKey(KeyCode.Escape)) {
             if (options == true) {
                 optionsPanel.SetActive(false);
                 options = false;
             } else {
                 Application.Quit();
             }
         }
 
         if (Input.GetKey(KeyCode.Menu)) {
             if (options == false) {
                 optionsPanel.SetActive(true);
                 options = true;
             }
         }
     }
 
     public void ButtonOptions () {
         if (options == true) {
             optionsPanel.SetActive(false);
             options = false;
         } else {
             optionsPanel.SetActive(true);
             options = true;
         }
     }
 
     public void ButtonQuit () {
         Application.Quit();
     }
 
     public void ButtonPlay () {
     }
 
     public void ToggleOptions (string opt) {
         Debug.Log("Toggled `" + opt + "` option.");
         if (opt == "music") {
             if (tog_music.isOn == true) tog_music.isOn = false;
             else tog_music.isOn = true;
             PlayerPrefs.SetInt("tog_music", (tog_music.isOn == true ? 1 : 0));
         } else {
             if (tog_sound.isOn == true) tog_sound.isOn = false;
             else tog_sound.isOn = true;
             PlayerPrefs.SetInt("tog_sound", (tog_sound.isOn == true ? 1 : 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by _creatio_ · Apr 21, 2015 at 02:47 PM

Writing to PlayerPrefs could be expensive if used multiple times sequencially. You may put Debug.Logs after each call to

PlayerPrefs.SetXXX

to check if you realy call it no more then you need.

Comment
Add comment · Show 2 · 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 Deinima · Apr 21, 2015 at 03:24 PM 0
Share

Thank you for the response. I found out that it's not actually the PlayerPrefs causing it as I initially thought. It is toggling the variable and UI element that seems to be causing the freezing.

avatar image lumbert · Aug 25, 2021 at 11:44 AM 0
Share

it is THAN, not THEN

avatar image
0

Answer by Afthrast · Nov 30, 2015 at 03:39 PM

Had quite amount of trouble with this ?bug?, till I managed to fix this thing. The problem seems to be not with the PlayerPrefs though. Not sure if this is universal, so feedback is appreciated, but it fixed every crash/freeze for me.

So for me, the toggle.isOn = true/false line caused the crashes, I used PlayerPrefs to determine if the option was toggled or not, and wanted to toggle the isOn, depending on the PlayerPrefs value, which I did very similarly to your code, and yielded half success, as somewhy it began crashing.

What I did was that I expanded the conditions under which the script should do the toggle.isOn, I checked the playerprefs and the state of the toggle in one if statement, now my code looks different from yours, but modifying your code would look something like this:

 ...
 if(PlayerPrefs.GetInt("tog_music") == 1 &&  tog_music.isOn ==  false) tog_music.isOn = true;
 else if(PlayerPrefs.GetInt("tog_music") == 0 &&  tog_music.isOn == true) tog_music.isOn = false;
 if(PlayerPrefs.GetInt("tog_sound") == 1 &&  tog_music.isOn ==  false) tog_music.isOn = true;
 else if(PlayerPrefs.GetInt("tog_sound") == 0 &&  tog_music.isOn ==  true) tog_music.isOn = false;
 ...
 if (tog_music.isOn == true) tog_music.isOn = false;
 else if(tog_music.isOn == false) tog_music.isOn = true;
 ...
 if (tog_sound.isOn == true) tog_music.isOn = false;
 else if(tog_sound.isOn == false) tog_music.isOn = true;
 ...

I know that the original code should work as well, but so should've mine, and didn't until I've became very specific with the conditions, I know this is an old question, but should anyone came across the same problem, please leave your feedback if this fixed your problem or not. The PlayerPrefs.SetInt should still work with the original code I believe.

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 Aarukka · Feb 06, 2016 at 02:19 PM 0
Share

I got the same problem too - handled it so sophisticated as not using the toggle at all. Good to know that the toggle is causing errors, all this time I thought is was my code although I did not find any errors in it. Thanks!

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

6 People are following this question.

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

Related Questions

Unity freezes when window inactive - any workaround? 1 Answer

iPhone app hangs in loading screen after upgrade to Xcode 4.0.2 and downgrading back to Xcode 3.5 for Unity 3.3 compatibility 0 Answers

app freezes when iphone keyboard appears 2 Answers

why is my character suddenly move when a scene is loaded? 1 Answer

Enemy stop moving when stared at 1 Answer


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