Newbie question here, how to attach a UI toggle to a static bool?

I want to directly change a bool by a toggle and can’t understand OnValueChange event very well. Also not so many tutorials on UI toggle for me so I cant learn it myself. Basically I just want the bool to be the status of the toggle and vice versa. The toggle have multiple copies and I want them all to adjust when one changed, that’s why I prefer them in static. Thanks!

In the Hierarchy, create an Empty GameObject called “UI Manager”

On that object, create a script called “UIManager”

Add the following script to UIManager:

using UnityEngine;

public class UIManager : MonoBehaviour
{
    bool myBool;

    public void OnToggleChanged(bool newValue)
    {
        myBool = newValue;
        print(myBool);
    }
}

Select your toggle in the Hierarchy

In the Inspector for your toggle, Click the plus sign in the On Value Changed box

Drag the UI Manager Game object from the Hierarchy to the “None (Object)” field

In the “No Function” field, click on the arrow to find UIManager > Dynamic Bool > OnToggle Changed

You can now run the Game and see that the updated value of the toggle is written to the console.

When implementing this in your own game, name all the OnToggleChanged methods appropriately (one for each Toggle), make sure they are Public so that they are exposed in the Inspector but otherwise, this should just work.

Any problems, come back to us as we’ll try and fix it for you…