- Home /
Toggle Event passing bool [4.6+]
So by reading the documentation for a toggle it says this "The Toggle has a single event called On Value Changed that responds when the user changes the current value. The new value is passed to the event function as a boolean parameter."
What this means to me is that I should be able to assign a function that receives a boolean parameter and it will pass it but this does not occur.
Simple function I would assign is like this
public void ToggleChanged(bool Value) { Debug.Log(Value); }
When assigned to the event I am only able to assign the value, so its either always going to be false or true, its not going to be the value which it has changed to.
So my question is what is the proper syntax for the function, I can't find any documentation on this.
Answer by Khaled-Khair · Feb 20, 2016 at 12:40 PM
Make sure you select the method located under the "Dynamic bool" header.
To clarify, this is on the dropdown from which you select what function to call.
@CptCheerios , accept $$anonymous$$haled's answer already!
The OP hasn't been seen since march 2016. So it's unlikely he will respond to your comment. Anyways i'll accept the answer.
Thanks. This is one of those Answers that has high Google search rankings and the only one that clarifies where the Dynamic Bool is located.
Answer by YoungDeveloper · Apr 11, 2015 at 02:13 AM
All what you get from toggle is the event. Passing bool manually will result in constant value as it's a hard-coded parameter. Of course you can set it by code, but in this case it's unneeded.
Instead, cache the toggle component;
using UnityEngine.UI; //include UI namespace before class declaration
public Toggle toggle; //drag from inspector
And set the event for value changed without parameters.
public void eventValueChanged(){
Debug.Log("Toggle is "+toggle.inOn); //check isOn state
}
I hope i made this understandable.
Also note that isOn will return the value of the toggle BEFORE the change.
Your answer
Follow this Question
Related Questions
Is there an event other than OnValueChanged for Toggle UI? 0 Answers
Toggle's 'On Value Changed' checkbox is shown for bool parameter function 0 Answers
How to have a callback when a toggle is clicked in a toggle group? 3 Answers
Is there a way to know when mouse is 'hovering' over a Toggle? 2 Answers
How do I get a toggle to dim its text when it's not "interactable"? 0 Answers