- Home /
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));
}
}
}
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.
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.
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.
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
Follow this Question
Related Questions
why screen freezes when i press the home button? Help! 0 Answers
Compound collider causing violation of freeze clause 0 Answers
Keep distance using NavMeshAgent causing freeze. 1 Answer
my mouse freezes when I hold down any key. 0 Answers
Freeze a Rigidbody2D's constraints and move the object manually with translate/Movetowards 1 Answer