Author Topic: UIToggle triggers OnValueChange upon SetActive  (Read 10566 times)

fanling3

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 29
    • View Profile
UIToggle triggers OnValueChange upon SetActive
« on: April 09, 2014, 10:11:41 PM »
I have an Option Menu in my game and a toggle button in it.

The option menu is hidden by default, and I uset SetActive(true) to display it when the user open this option menu, at the same time, the toggle button in this menu will trigger the function inside On Value Change, even the user has not clicked on it yet.

How to fix this? Thanks.

tr4np

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: UIToggle triggers OnValueChange upon SetActive
« Reply #1 on: April 09, 2014, 10:52:41 PM »
I just ran into this myself.  What's happening is that it's calling your method with a value of 0 when it initializes.  This is what I did to resolve this:

1. add an int parameter to the callback method.  Add this to the beginning of the method: if the value is 0, just return (i.e. ignore it).
2. in the inspector, the int parameter will need to be provided.  Drag your UIToggle to it, then select "value" from the property list

fanling3

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: UIToggle triggers OnValueChange upon SetActive
« Reply #2 on: April 09, 2014, 11:38:14 PM »
I just ran into this myself.  What's happening is that it's calling your method with a value of 0 when it initializes.  This is what I did to resolve this:

1. add an int parameter to the callback method.  Add this to the beginning of the method: if the value is 0, just return (i.e. ignore it).
2. in the inspector, the int parameter will need to be provided.  Drag your UIToggle to it, then select "value" from the property list
I think it's not the case.

The button in my option menu is actually a Music On Off button. And I stored the parameter inside PlayerPref in Unity.

What I can see now is: When I play the Editor, the music is on, then I open the option menu, the music is OFF. I did NOT toggle the button and stop the Editor, and play it again. The music is still off. Then I open the option menu again, and the music is ON.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIToggle triggers OnValueChange upon SetActive
« Reply #3 on: April 10, 2014, 07:15:37 AM »
Sounds like you are not checking UIToggle.current.value inside your callback. You can't just flip the value every time you get the callback. You need to respect its state. The callback will be triggered as soon as the toggle is enabled, so that the callback is in the same state as the toggle on start.

OnChange doesn't mean "flip the state". It means "state may have changed, so run your logic after checking the current state".

fanling3

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: UIToggle triggers OnValueChange upon SetActive
« Reply #4 on: April 11, 2014, 05:39:30 AM »
As you said, I am really using MusicOn = !MusicOn; in the callback function. But I am not quite understand what do you mean by "run your logic after checking the current state", as the callback function is called no matter on start or when the user clicks on it. How can I recognize these 2 conditions in the callback function?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIToggle triggers OnValueChange upon SetActive
« Reply #5 on: April 11, 2014, 07:30:42 AM »
  1. bool myState = UIToggle.current.value;
There's your state. Don't flip any values. Use the provided state value.

fanling3

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: UIToggle triggers OnValueChange upon SetActive
« Reply #6 on: April 14, 2014, 01:36:23 AM »
  1. bool myState = UIToggle.current.value;
There's your state. Don't flip any values. Use the provided state value.
Here is my current callback function:
  1. musicOn = !musicOn; //the boolean
  2. musicButton.value = musicOn; //this is newly added according to what you said, musicButton is the UIToggle object
  3. bgSource.mute = !musicOn; //the audio srouce object
Now the music can be on off without the problem mentioned before, but the button state transition is still wrong (the foreground sprite in State Transition in UIToggle). musicOn is true by default, the music is properly playing on start now but the button sprite is off (the alpha is zero), and there is a "StackOverflowException: The requested operation caused a stack overflow." raised at the same time, on line EvevntDelegate.cs:404, Not sure what's the probem?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIToggle triggers OnValueChange upon SetActive
« Reply #7 on: April 14, 2014, 03:16:48 AM »
I just said don't flip any values, and you're still flipping the value:
Quote
musicOn = !musicOn; //the boolean
This is backwards and is not needed:
Quote
musicButton.value = musicOn
Your code should be just one line:
  1. bgSource.mute = !UIToggle.current.value;

fanling3

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: UIToggle triggers OnValueChange upon SetActive
« Reply #8 on: April 15, 2014, 01:50:50 AM »
I just said don't flip any values, and you're still flipping the value:This is backwards and is not needed:Your code should be just one line:
  1. bgSource.mute = !UIToggle.current.value;
It works. But I am not quite really understand how it works. Is it when I read the current.value in the callback function, current.value will flip itself?
« Last Edit: April 15, 2014, 01:57:32 AM by fanling3 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIToggle triggers OnValueChange upon SetActive
« Reply #9 on: April 15, 2014, 09:24:26 AM »
The flipping happens inside the toggle class. That's what it does.

current.value just tells you its value.