Class Switch
Switch interactable that executes a function on another script when activated. Used for opening doors, unlocking chests, stopping or engaging traps, and similar mechanisms. The target object must implement ISwitchTarget.
[RequireComponent(typeof(SceneObjectNamer))]
public class Switch : Interactable, ISpawnable, ISwitch, IInteractable, ISceneObject
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourNetworkBehaviourSwitch
- Implements
- Inherited Members
-
NetworkBehaviour.IsSpawnedNetworkBehaviour.ComponentIndexNetworkBehaviour.NetworkObjectNetworkBehaviour.MAXIMUM_NETWORKBEHAVIOURSNetworkBehaviour.UNSET_NETWORKBEHAVIOUR_IDNetworkBehaviour.ToString()NetworkBehaviour.Reset()NetworkBehaviour.OnValidate()NetworkBehaviour.IsBehaviourReconcilingNetworkBehaviour.ClearReplicateCache()NetworkBehaviour.RequestReconcileSends()NetworkBehaviour.CreateReconcile()NetworkBehaviour.Reconcile_Reader_Remote<T>(PooledReader, ref T)NetworkBehaviour.ClearBuffedRpcs()NetworkBehaviour.IsClientOnlyNetworkBehaviour.IsServerOnlyNetworkBehaviour.IsHostNetworkBehaviour.IsClientNetworkBehaviour.IsServerNetworkBehaviour.IsDeinitializingNetworkBehaviour.NetworkManagerNetworkBehaviour.ServerManagerNetworkBehaviour.ClientManagerNetworkBehaviour.ObserverManagerNetworkBehaviour.TransportManagerNetworkBehaviour.TimeManagerNetworkBehaviour.SceneManagerNetworkBehaviour.PredictionManagerNetworkBehaviour.RollbackManagerNetworkBehaviour.NetworkObserverNetworkBehaviour.IsClientInitializedNetworkBehaviour.IsClientStartedNetworkBehaviour.IsClientOnlyInitializedNetworkBehaviour.IsClientOnlyStartedNetworkBehaviour.IsServerInitializedNetworkBehaviour.IsServerStartedNetworkBehaviour.IsServerOnlyInitializedNetworkBehaviour.IsServerOnlyStartedNetworkBehaviour.IsHostInitializedNetworkBehaviour.IsHostStartedNetworkBehaviour.IsOfflineNetworkBehaviour.IsNetworkedNetworkBehaviour.GetIsNetworked()NetworkBehaviour.IsManagerReconcilingNetworkBehaviour.ObserversNetworkBehaviour.IsOwnerNetworkBehaviour.IsControllerNetworkBehaviour.HasAuthorityNetworkBehaviour.OwnerNetworkBehaviour.OwnerIdNetworkBehaviour.ObjectIdNetworkBehaviour.LocalConnectionNetworkBehaviour.OwnerMatches(NetworkConnection)NetworkBehaviour.Despawn(GameObject, DespawnType?)NetworkBehaviour.Despawn(NetworkObject, DespawnType?)NetworkBehaviour.Despawn(DespawnType?)NetworkBehaviour.Spawn(GameObject, NetworkConnection, Scene)NetworkBehaviour.Spawn(NetworkObject, NetworkConnection, Scene)NetworkBehaviour.RemoveOwnership()NetworkBehaviour.GiveOwnership(NetworkConnection)NetworkBehaviour.GetInstance<T>()NetworkBehaviour.TryRegisterInstance<T>(T)NetworkBehaviour.UnregisterInstance<T>()NetworkBehaviour.CanLog(LoggingType)NetworkBehaviour.OnStartServerCalledNetworkBehaviour.OnStartClientCalledNetworkBehaviour.OnStartNetwork()NetworkBehaviour.OnStopNetwork()NetworkBehaviour.OnOwnershipServer(NetworkConnection)NetworkBehaviour.OnSpawnServer(NetworkConnection)NetworkBehaviour.OnDespawnServer(NetworkConnection)NetworkBehaviour.OnStartClient()NetworkBehaviour.OnStopClient()NetworkBehaviour.OnOwnershipClient(NetworkConnection)MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.didStartMonoBehaviour.didAwakeMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.GetComponentIndex()Component.CompareTag(TagHandle)Component.transformComponent.transformHandleComponent.gameObjectComponent.tagObject.GetEntityId()Object.MemberwiseClone()Object.GetInstanceID()Object.GetHashCode()Object.InstantiateAsync<T>(T)Object.InstantiateAsync<T>(T, Transform)Object.InstantiateAsync<T>(T, Vector3, Quaternion)Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Scene)Object.Instantiate<T>(T, InstantiateParameters)Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.FindObjectsByType<T>()Object.FindObjectsByType<T>(FindObjectsInactive)Object.nameObject.hideFlags
Fields
AchievementTemplate
Achievement to increment when a player operates this switch.
public AchievementTemplate AchievementTemplate
Field Value
IsToggle
When true, the switch toggles between activated and deactivated states. When false, the switch can only be activated once.
[Tooltip("When true, the switch toggles between activated and deactivated states.")]
public bool IsToggle
Field Value
Properties
SwitchTarget
Cached reference to the resolved ISwitchTarget on the target GameObject.
public ISwitchTarget SwitchTarget { get; }
Property Value
Title
Display title shown above the switch.
public override string Title { get; }
Property Value
TitleColor
Title color for the switch UI label.
public override Color TitleColor { get; }
Property Value
- Color
Methods
BroadcastState(IInteractable, ISwitchTarget)
Tells the observers of the switch's NetworkObject the target's state (server only): world state, so sent to everyone who can see the switch, not to whoever threw it.
public static void BroadcastState(IInteractable interactable, ISwitchTarget switchTarget)
Parameters
interactableIInteractableswitchTargetISwitchTarget
Remarks
The NetworkObject that owns the switch, not one on the switch's own GameObject. A generated door's switch is on Door N/Leaf/Panel and its NetworkObject on Door N: looked up with GetComponent (2026-11-13) no broadcast was ever sent, the server opened the door (its leaf no longer solid) while every client kept it shut and solid, walking in was corrected through a closed door, and the next press closed it again unseen.
CanInteract(IPlayerCharacter)
Returns true if the specified player character may interact with this object.
public override bool CanInteract(IPlayerCharacter character)
Parameters
characterIPlayerCharacterThe player character attempting to interact.
Returns
- bool
True if interaction is allowed, false otherwise.
Remarks
A pure question with no side effects. It used to stamp the character's interact rate-limit as part of answering, which made it unusable as a check: the scene server, the quest system and the client's input handler all call it, and each call silently consumed a limiter meant for one of the others. A quest accepted within the limiter's window of the interaction that opened the dialogue was refused by a debounce nobody intended to spend, and the refusal path sends nothing back — so the player saw a dead keypress. Spend the limiter explicitly with TryConsumeInteractRateLimit(IPlayerCharacter).
OnAwake()
Called when the object is awakened. Override to implement custom initialization logic.
public override void OnAwake()
OnStartServer()
Registers this interactable in the scene object registry and assigns its ID.
public override void OnStartServer()
Remarks
Deliberately not in Awake under #if UNITY_SERVER, which is where it used
to live. UNITY_SERVER is a build-target define, and the scene server also runs
from the editor — where it is undefined. Every interactable therefore skipped
registration there, kept ID 0, and sent 0 to every client in WritePayload(NetworkConnection, Writer);
the server's own registry stayed empty, so ValidateSceneObject refused every
interaction in the world. Nothing could be talked to, looted, or bound to outside a
dedicated server build. This is the same gate that had already been found and removed
from NPC.OnStartServer, for the same reason.
OnStartServer() is the correct home: FishNet invokes it only on a peer that is actually running a server, and it runs before the spawn message — and therefore before WritePayload(NetworkConnection, Writer) — is built, so the ID a client receives is the one assigned here. Re-registration on pool reuse is a no-op that preserves the existing ID.
OnStopServer()
Called on the server before deinitializing this object.
public override void OnStopServer()
ReadPayload(NetworkConnection, Reader)
Reads the switch's state and drives the target to match it.
public override void ReadPayload(NetworkConnection connection, Reader reader)
Parameters
connectionNetworkConnectionreaderReader
WritePayload(NetworkConnection, Writer)
Writes the switch's current state so a client that arrives later sees the right pose.
public override void WritePayload(NetworkConnection connection, Writer writer)
Parameters
connectionNetworkConnectionwriterWriter
Remarks
SwitchStateBroadcast only reaches clients that were already observing when the
switch was thrown. Without the state in the payload, a player walking into a room whose
door was opened an hour ago finds it shut on their screen alone — and pressing the
switch then closes it for everyone else, because the server's copy was open all
along. This is the same gap NPCs had with their dead flag, and the same fix.