Global Toggle Switch (Synced)

This script toggles an object on/off for everyone in the instance using a synced boolean.

Category: Networking Systems

Use Case

  • Stage lights on/off
  • Global decor visibility
  • Shared room state switch

UdonSharp Script

using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;

public class GlobalToggleSwitch : UdonSharpBehaviour
{
    [Header("Object enabled/disabled globally")]
    public GameObject targetObject;

    [UdonSynced] private bool isOn = true;

    private void Start()
    {
        ApplyState();
    }

    public override void Interact()
    {
        if (targetObject == null)
        {
            return;
        }

        Networking.SetOwner(Networking.LocalPlayer, gameObject);
        isOn = !isOn;
        ApplyState();
        RequestSerialization();
    }

    public override void OnDeserialization()
    {
        ApplyState();
    }

    private void ApplyState()
    {
        targetObject.SetActive(isOn);
    }
}

License:

Unity Setup

  1. Create a button/switch object with collider.
  2. Add VRC_Interactable.
  3. Add this UdonSharp behaviour.
  4. Assign the object you want to toggle in targetObject.
  5. Ensure object sync mode supports your expected behavior.

Notes

  • Ownership is transferred to the interacting player before serialization.
  • Keep one authoritative toggle script per target to avoid state conflicts.

Extra Tips and Troubleshooting

Tips and Tricks

  • Use one synced script as state authority per feature.
  • Pair toggle state with visual indicator objects.

Troubleshooting

  • If state desyncs, confirm ownership transfer and serialization call order.

Related Content

Prefab Setup Notes

Import the prefab or script into a throwaway test scene before adding it to a live world. Confirm inspector references, ownership behavior, sync, triggers, UI hooks, and audio or object links before moving it into the production scene.

Testing Checklist

  • Test once as a local player and again with a second client or late joiner if the behavior can affect more than one player.
  • Confirm ownership, sync, trigger zones, UI references, and audio or object references are assigned intentionally.
  • Check desktop and VR interaction distance so players can actually use the feature in context.
  • Keep a backup of the scene before changing prefabs, UdonBehaviours, or serialized references.

Related Pages

Official References