- Home /
[Unity3d beta 4.6] Make Active for UnityEngine.UI.Toggle
I am playing around with the 4.6beta and UnityEngine.UI. I have a ToggleGroup setup and I want to select a toggle in code after I load some PlayerPrefs for which toggle was selected last time.
I found the toggle that I want to activate, but I do not know how to make it active. I looked through the scripting reference for Toggle and ToggleGroup but I do not see any call that accomplishes this. I thought that UnityEngine.UI.ToggleGroup.NotifyEnabled(Toggle t) would do it, but that did not work.
It is possible that sending a ToggleEvent is required... but I do not quite understand the eventing system in Unity enough to evaluate that option.
Does anyone know how to tell a toggle to become active in code?
Answer by AyAMrau · Aug 31, 2014 at 08:35 PM
NotifyEnabled seems to only be used to inform the group that the Toggle is enabled.
So if you want to actually change a state of a single Toggle you just change it's isOn value:
toggle.isOn = true;
I think I was writing my rant and didn't reload the page. Thanks for the more concise answer. I'm check marking yours. I submitted my comments to the feedback site.
Setting this does not work 100%.
I have a toggle group and when selecting a toggle in code after I load some the value from PlayerPrefs only the check for the toggle is set properly.
I have 3 toggles and each toggle has a different transition color tint. When setting the toggle in code, the check mark is set properly with the correct color tint but the background sourceImage color tint is not.
The background sourceImage color tint is only set when pressing the toggle.
Answer by kr77 · Aug 31, 2014 at 08:47 PM
I found the answer to my own question just a moment ago. I needed to set the Toggle.isOn property to true. The name confused me. I bypassed the property due to assuming that it was a function that returned a bool; like EventSystems.UIBehaviour.IsActive or UI.Selectable.IsInteractable.
At this time the documentation for the Toggle.isOn call is:
Is the toggle on.
This sounds like a method that returns a bool, but then it is clearly referenced as an actual bool property when you click into it. Maybe a better label could be used to identify it as an actual value. Like 'selected' which would be like the enabled property on many Unity objects.
After looking for instances of properties and methods starting with 'is', there appears to be no standard usage of the word 'is' in naming. The word 'on' for example has the standard usage of being a method that reacts to an event.
Your answer
Follow this Question
Related Questions
[4.6] Toggle/Checkbox On/Off 1 Answer
Use a toggle button to change a GameObjects visibility 2 Answers
Toggles/Toggle Groups Question 0 Answers
Cant access .interactable 1 Answer
Toggle UI 4.6 0 Answers