Hi I am creating a game using slider to control my player.
So, I want to create an abnormal status where whenever i move slider to the right, it goes to the left and vice versa.
Is this possible? This is my code:
This throws me an error of StackOverflowException.
Any help would be greatly appreciated.
Thanks!
PS. math is bad, i usually fix it after i have it working. lol
The “stack” is the data structure that contains the current method calls. Method A calls method B calls method C, so these three are stacked on top of each other. When method C is done, it’s removed from the stack, and method B continues where it paused to let method C run.
A StackOverflowException happens when you are calling method after method, the stack filling and filling, with no end. This happens in “endless recursion”. In your case, I assume that you call ReverseValue() in the slider’s onChangeValue event. You then do slider.value = ..., which triggers the onChangeValue event, which then triggers this method again. And voila, there’s Unity doing this until the stack is full.
The general fix is to not change the slider’s value in the method that is called when the slider’s value is changed. In your case specifically, you should probably manipulate another float value than the slider value. It doesn’t make too much sense anyway, since the slider would just jump under the cursor/finger like crazy.
Thanks for the explanation! After thinking overnight, i thought of what you said. Instead of making my player the handle of the slider, i will control it outside the slider.