- Home /
The requested operation caused a stack overflow
Hey, I'm writing a simple android game and I have this problem because I want to make that if one Toggle is selected, the other one turns off. I did it this way, as you can see below, but I get the error "The requested operation caused a stack overflow". Each click on the Toggle causes the game to jam for 1 - 2 seconds, and after some 4 clicks, the game crashes. Please, because I don't know how to solve it
using System.Collections;
using System.Collections.Generic;
using System.Runtime;
using UnityEngine;
using UnityEngine.UI;
public class Menu : MonoBehaviour
{
public GameObject playObj;
public GameObject optionsObj;
public GameObject exitObj;
public GameObject owneObj;
public Slider volume;
public Toggle roadLeft;
public Toggle roadRight;
public void PlayButton()
{
playObj.SetActive(true);
}
public void OptionsButton()
{
optionsObj.SetActive(true);
}
public void OwneButton()
{
owneObj.SetActive(!owneObj.activeInHierarchy);
}
public void SumbitButton()
{
optionsObj.SetActive(!optionsObj.activeInHierarchy);
}
public void ExitButton()
{
exitObj.SetActive(true);
}
public void InfoButton()
{
Application.OpenURL("...");
}
//Options
void Start()
{
volume.value = AudioListener.volume;
volume.onValueChanged.AddListener(delegate { OnVolumeValueChange(); });
roadRight.GetComponent<Toggle>();
roadLeft.GetComponent<Toggle>();
}
public void OnVolumeValueChange()
{
AudioListener.volume = volume.value;
}
public void OnDirectionValueChangeLeft()
{
roadLeft.isOn = false;
roadLeft.isOn = true;
}
public void OnDirectionValueChangeRight()
{
roadRight.isOn = false;
roadRight.isOn = true;
}
}
These two lines do nothing of consequence:
roadRight.GetComponent<Toggle>();
roadLeft.GetComponent<Toggle>();
Tą linijkę:
volume.onValueChanged.AddListener(delegate { OnVolumeValueChange(); });
możesz zapisać nieco krócej jako:
volume.onValueChanged.AddListener( OnVolumeValueChange );
Jeśli, tą tu metodę:
public void OnVolumeValueChange ()
{
AudioListener.volume = volume.value;
}
zmienisz na:
public void OnVolumeValueChange ( float vol )
{
AudioListener.volume = vol;
}
aby pasowała do sygnaturki danego eventu (tu: UnityAction<float>
).
Alternatywnie można to też zamienić na:
volume.onValueChanged.AddListener(
(float vol) => { AudioListener.volume = vol; }
);
ale nie polecam, bo można tego (pisowni lambda expression po PL funkcja ano$$anonymous$$owa) łatwo nadużyć i przysporzyć tylko kłopotów na potem. Warto głównie tylko wiedzieć.
Answer by andrew-lukasik · Nov 14, 2020 at 09:57 PM
Stack overflow
is, most often than not, an infinite loop exception. My guess is that this code below is responsible:
public void OnDirectionValueChangeLeft ()
{
roadLeft.isOn = false;// probably leads to OnDirectionValueChangeLeft being called again
roadLeft.isOn = true;// probably leads to OnDirectionValueChangeLeft being called again
}
public void OnDirectionValueChangeRight ()
{
roadRight.isOn = false;// probably leads to OnDirectionValueChangeRight being called again
roadRight.isOn = true;// probably leads to OnDirectionValueChangeRight being called again
}
Because OnDirectionValueChangeLeft
surely reads like something that is being called by this roadLeft
Toggle
. If so this will create a call in a loop, ad infinitum, causing cpu freeze and/or crash.
You're welcome.
Gdyby coś było wciąż nie jasne to pytaj śmiało.
Your answer
![](https://koobas.hobune.stream/wayback/20220613015727im_/https://answers.unity.com/themes/thub/images/avi.jpg)