Skip to main content
‹ Back to Tutorials
Intermediate Unity

Unity VR XR Interaction Toolkit - Turn Light on off w/Socket

Aug 25, 2022
About this tutorial

In this Unity VR XR Toolkit tutorial, I'll show how to use an Activation Event that will allow you to Turn Light on w/Socket has the electric box attached and off when you remove it. I'll also show how to play a sound as well. ► Art Assets - Polygon City - Low Poly https://bit.ly/3ScOYa1 My disco

Written Guide

This VR tutorial wires a street light to an XR socket: snap the electric box onto the pole's socket and the light switches on with an electric crackle; pull it off and the light dies. No code — everything runs through the XR Socket Interactor's built-in events and Unity's inspector.

Step 1 — Set up the light

The street light is a game object holding two lights: a spotlight aimed down to illuminate the ground, and a point light that makes the lamp fixture itself glow. Keep this object inactive in the scene — the socket events will activate it. Everything hinges on flipping this one object on and off.

Step 2 — Pick the right interactor event

Select the pole base — the object with the XR Socket Interactor that the electric box attaches to. The interactor exposes hover and select events: Hover Entered/Exited fire when something merely floats inside the trigger area, while Select Entered fires when an object actually attaches to the socket. Attachment is what should power the light, so Select Entered is the one you want.

Step 3 — Turn the light on when the box attaches

Click the plus under Select Entered, drag the spotlight object into the slot, and from the function dropdown (showing No Function) choose GameObject, then SetActive. The checkbox next to it is the value that gets passed: checked activates the object, unchecked deactivates it. Check it — attach the box, light comes on.

Step 4 — Add a 3D electric sound

To play a sound on attach, the box base first needs an Audio Source component — add one, but leave the clip empty since the interactor event will supply it. Two settings matter: uncheck Play On Awake, and drag Spatial Blend from 2D fully over to 3D. That last one is what sells the effect in VR — a 2D sound plays evenly in both ears, but a 3D sound plays from its position in space, so turning your head toward the box makes the hum favor that ear, just like a real fixture across the room. Back in Select Entered, add a second entry with the plus, target the pole box, and choose AudioSource → PlayOneShot, then pick the electricity clip via the little dot icon.

Step 5 — Turn it off on detach

First test: grab the box, drop it onto the socket, hear the crackle, watch the ground light up — then pull the box off and the light stubbornly stays on. The fix is the mirror image of step 3: under Select Exited, add the spotlight with GameObject → SetActive and leave the checkbox unchecked. You could add a power-down sound here too, but silence works. Test again: attach, light and sound; detach, dark. A socket, two events, and one checkbox each way — that's a complete interactive circuit for your VR scene.

Unity tools by Bliz Studio
Built for our own games — now on the Asset Store

Multi Tag — unlimited hierarchical tags for any GameObject. Hierarchy Pro — style your whole hierarchy with rules. Made by a Unity dev, for Unity devs.

Explore our Unity assets →