- Home /
Access UI.Toggle from c# CodeBehind -How does one set the Toggle created in the front end of Unity?
I would like to set the IsOn Property to True. The API states that one can only get the "IsOn" values of when it was instatiated. There is no setter for the property it seems.
GameObject togGameObj= GameObject.Find("MyToggle");
Toggle myTog = togGameObj.GetComponent<Toggle>();
myTog.IsOn = true;
Answer by B3aT · Mar 16, 2015 at 02:50 PM
I do not think you can control the UI elements directly, you must use the EventSystem class. So trigger it old fashion web way, simulate a click.
ExecuteEvents.Execute<IPointerClickHandler>(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
Hi B3aT, I can change the value of a "Text" UI control directy. The following example works fine. So I assume there could still be a similar functionality with "Toggle" UI control:
GameObject textGameObj= GameObject.Find("myText");
Text myText = textGameObj.GetComponent<Text>();
$$anonymous$$yText.Text = "Hello World"
Yes but that is a property. The toggle is a state, and in all articles/videos/tutorial you can see is all about the new eventSystem. Theres even an example where a character and interact with the world space UI canvas by touching it, using events.
HI B3at,
What Articles/Videos/Tutorials please, can you reference what your quoating here?
How I understand it from a Program$$anonymous$$g perspective, Toggle is a class, the IsOn is a property with getter and a setter. $$anonymous$$y issue is that the Setter for the IsOn property doesn't seem to do anything with the Property.
I guess my question now becomes is there another property or even function that allows one to set the IsOn Property from the codebehind.
Hi, One is when they first presented the UI (at a conference, do not have the time to search for it), and the tutorials sections presents how you work with the UI, all events go trough EventSystem (OnClick attach callbacks).
As your answer, no there is no way to do this. http://docs.unity3d.com/ScriptReference/UI.Toggle.html Like there is no way to click on a button http://docs.unity3d.com/ScriptReference/UI.Button.html