Accessing toggle's variable IsOn

In my game I used new UI functionality to create toggle element.
I can change it’s value by changing variable IsOn in the Unity’s interface.
But how can I acess it from the script?

I want to check, if togle=true, then do smth.


GameObject toggle;

void GetToggle(){
	toggle = GameObject.Find ("Toggle").GetComponent<ToggleScript> ();
	toggle.isOn = true;

public Toggle MyToggle = null;

and NOT

public Gameobject MyToggle = null;

Just to explain what you did wrong is.

toggle is of type GameObject.

Then you use GameObject.Find that is really a bad practice (use public field for example is better).

And more you are using GetComponent of ToggleScript. That won’t work. ToggleScript is NOT a of type GameObject. That is the same as Car car = new Plane()…

Finally toggle.isOn will never work. toggle is of type GameObject which doesn’t have ANY field called isOn.

That is why you need something of type Toggle (don’t forget to use using UnityEngine.UI;)