How do you pass a bool for a Toggle to a function?

im trying to get a Toggle to turn something on and off
how do you pass a bool in so its true when on false when off

public void eventValueChanged(bool sails)

1 Like

…also, if you want to set the value without getting a callback:

I think he is trying to turn something else on/off when the toggle is clicked, not modify the value of the toggle. You could add a method to be called by an event when the toggle is pressed. You might find it easier to wrap your head around by just checking the value of the toggle each frame, and acting on it when you notice it changed from last frame. If you prefer that approach, it might look like this.

public UI.Toggle MyToggle;
private bool lastToggleValue;  //stores the value of the toggle from last frame

void Start()
{
    lastToggleValue = MyToggle.isOn;
}

void Update()
{
    if (lastToggleValue != MyToggle.isOn)
    {
        turnSomethingOnOrOff(-MyToggle.isOn);  //Note the "-" minus sign, since you said you wanted it to do whatever the opposite value the toggle is
        lastToggleValue = MyToggle.isOn;
    }
}

void turnSomethingOnOrOff(bool onOrOff)
{
    //Here you would actually use the bool onOrOff to do whatever you want to do with it

}

Got it sorted guys thanks, my ship has sails now

1 Like