- Home /
Possible Editor bug : Toggle crash / image not updating in editor
Hello, I really need help on this one, I have been seeking for solution and scroll hundreds of line of code just to see any mistake, but it's just a very basic toggle that produce the crash. Everything was working fine in past session but today i retest a toggle and it crash.
I have a serie of toggle that crash (hang) editor in play mode.
void Awake(){
EasyBtn.onValueChanged.AddListener(delegate { OnDifficultyChangeClick(1); });
HardBtn.onValueChanged.AddListener(delegate { OnDifficultyChangeClick(2); });
VeryHardBtn.onValueChanged.AddListener(delegate { OnDifficultyChangeClick(3); });
}
void OnDifficultyChangeClick(int MyChoice)
{
Debug.Log("My difficulty choice is " + MyChoice);
if (MyChoice == 1)
{
EasyBtn.isOn = true;
HardBtn.isOn = false;
VeryHardBtn.isOn = false;
InsaneBtn.isOn = false;
CurrentDifficultyChoice = 1;
}
else if (MyChoice == 2)
{
EasyBtn.isOn = false;
HardBtn.isOn = true;
VeryHardBtn.isOn = false;
InsaneBtn.isOn = false;
CurrentDifficultyChoice = 2;
}
else if (MyChoice == 3)
{
EasyBtn.isOn = false;
HardBtn.isOn = false;
VeryHardBtn.isOn = true;
InsaneBtn.isOn = false;
CurrentDifficultyChoice = 3;
}
else if (MyChoice == 4)
{
EasyBtn.isOn = false;
HardBtn.isOn = false;
VeryHardBtn.isOn = false;
InsaneBtn.isOn = true;
CurrentDifficultyChoice = 4;
}
else
{
Debug.Log(MyChoice);
}
}
The DebugLog doesn't even get called, so it look like the problem is with the listener !?
The weird thing I see in the editor is that the image preview in the inspector doesn't show up for the problematic toggle. The image preview is blank (alpha). When I uncheck and recheck the gameobject the preview regenerate correctly. When I look in window sprite packer the correct image is there, I thought it could be a corrupted sprite pack, but I don't know how to force a regeneration. I also tried reimporting the image and deleting the meta to force recreation but nothing have improved.
Any clue ?
Answer by Unplug · Jan 30, 2018 at 08:02 PM
So one solution is to rely on the name of the item being click instead of just pushing an integer as the name of the object being clicked won't change no matter if the value of any toggle change. The disadvantage of this, is that the "onvaluechange" still trigger and you loose some computing power for no reason, but considering the low amount of toggle and speed of such action, it is not a problem. Maybe someone will come up with a better solution or maybe unity will allow us to use a onClick function for toggle...
void Awake()
{
EasyBtn.onValueChanged.AddListener(delegate { OnDifficultyChangeClick(EventSystem.current.currentSelectedGameObject.name); });
HardBtn.onValueChanged.AddListener(delegate { OnDifficultyChangeClick(EventSystem.current.currentSelectedGameObject.name); });
VeryHardBtn.onValueChanged.AddListener(delegate { OnDifficultyChangeClick(EventSystem.current.currentSelectedGameObject.name); });
InsaneBtn.onValueChanged.AddListener(delegate { OnDifficultyChangeClick(EventSystem.current.currentSelectedGameObject.name); });
}
void OnDifficultyChangeClick(string ToggleName)
{
Debug.Log("My difficulty choice is " + ToggleName); //I have 3 toggle, so the whole loop get called twice. Since the togglename doesn't change without a new click, the loop end.
if (ToggleName == EasyBtn.name)
{
EasyBtn.isOn = true;
HardBtn.isOn = false;
VeryHardBtn.isOn = false;
InsaneBtn.isOn = false;
CurrentDifficultyChoice = 1;
}
else if (ToggleName == HardBtn.name)
{
EasyBtn.isOn = false;
HardBtn.isOn = true;
VeryHardBtn.isOn = false;
InsaneBtn.isOn = false;
CurrentDifficultyChoice = 2;
}
else if (ToggleName == VeryHardBtn.name)
{
EasyBtn.isOn = false;
HardBtn.isOn = false;
VeryHardBtn.isOn = true;
InsaneBtn.isOn = false;
CurrentDifficultyChoice = 3;
}
else if (ToggleName == InsaneBtn.name)
{
EasyBtn.isOn = false;
HardBtn.isOn = false;
VeryHardBtn.isOn = false;
InsaneBtn.isOn = true;
CurrentDifficultyChoice = 4;
}
else
{
Debug.Log(ToggleName);
}
}
Answer by Commoble · Jan 30, 2018 at 06:16 AM
When Unity hangs with no error message or crash log, it typically means you've caused an infinite loop. If I'm reading your code correctly, your listeners call your OnDifficultyChangeClick method whenever the values of your button change. This function then changes the values of each of your buttons, which causes OnDifficultyChangeClick to be called again for each of those buttons., which changes the value of each of your buttons again. This is an infinite loop.
You'll need to rework how your interface works; you probably shouldn't have your buttons directly altering your other buttons. One suggestion (only the first thing I thought of, not necessarily the best) have the buttons just change CurrentDifficultyChoice, and then have the buttons check that value in an update loop and update their appearance accordingly.
$$anonymous$$mmmmh i wasn't seeing this as a loop since there is no for and while and that the function is called with an integer and I was treating that integer in the if and the integer doesn't change. That said, the value of the toggle get change and that is definitely recalling the toggle function over and over !! Thanks for the heads up. I'll post a solution. It was first buttons, not that it is toggle, I will consider using a toggle group to fix the situation and i'll repost the solution
Your answer
