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);
}
}
Unity Setup
- Create a button/switch object with collider.
- Add
VRC_Interactable. - Add this UdonSharp behaviour.
- Assign the object you want to toggle in
targetObject. - 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.
