Common components
Updated
A few components belong to almost every entity. Transform says where the entity is, Enable whether it is on, Layer which layer it is on, and Lock whether the editor can pick it. Three more are common enough to learn early: Billboard turns an entity toward the camera, Group recenters a group on its children, and Shadow decides whether meshes cast and receive shadows. This page documents each one, with its props, its events and how a script uses it.
Before you start
Section titled “Before you start”- You know what components are and how
getComponentandsetPropswork. See Components. - To hear the events listed here, the entity needs a Signal Emitter. See Events and signals.
At a glance
Section titled “At a glance”Every entity except the scene root is an element. Every element carries three components from the moment it is created, and they can never be removed: Enable, which switches the entity and its subtree on or off; Layer, which puts the entity on a render and raycast layer (see Layers); and Lock, which stops the editor from picking or transforming the entity. Most element types also start with a Transform, which holds the position, rotation and scale. This page documents all four, and the other components almost every entity shares:
| Component | Type | On | Script shortcut |
|---|---|---|---|
| Transform | comp- |
Most entity types; cannot be disabled | setPosition, setRotation, setScale, setLookAt |
| Enable | comp-enable |
Every element; required | set, isEnabled() |
| Layer | comp-layer |
Every element; required | setLayer(n) |
| Lock | comp-lock |
Every element; required | setLocked(on) |
| Billboard | comp- |
Optional | none |
| Group | comp-group |
Group entities only | none |
| Shadow | comp-shadow |
Optional; default on meshes | none |
Transform
Section titled “Transform”comp-transform holds the entity’s local position, rotation and scale: where it is relative to its parent. It is a live view of the entity’s own position, rotation and scale, so the scene always saves where the entity is now. Most entity types start with a Transform and require it; it cannot be removed or disabled.
| Prop | Type | Default | Description |
|---|---|---|---|
position |
{ x, y, z } |
0, 0, 0 |
Local position, in metres from the parent. |
rotation |
{ x, y, z } |
0, 0, 0 |
Local rotation, as Euler angles in radians. |
scale |
{ x, y, z } |
1, 1, 1 |
Local scale on each axis. |
lockPosition |
{ x, y, z } booleans |
all false |
Position axes the editor’s gizmo cannot move. |
lockRotation |
{ x, y, z } booleans |
all false |
Rotation axes the editor’s gizmo cannot turn. |
lockScale |
{ x, y, z } booleans |
all false |
Scale axes the editor’s gizmo cannot resize. |
Moving an entity
Section titled “Moving an entity”There are two ways to move an entity from a script:
- The setters:
entity.setPosition(x, y, z),setRotation(x, y, z)(Euler radians),setScale(x, y?, z?)andsetLookAt(x, y, z)(face a world point). They move the entity’s physics body and colliders with it, and send the Transform events. They return the entity, so they chain:door.setPosition(0, 0, 2).setRotation(0, Math.PI, 0). - Direct writes:
entity.position.x += 1,entity.rotation.y = angle. They are the quickest way, fine for a purely visual object moved every frame. They send no events and leave colliders behind.
Use the setters whenever something else must follow the move: physics, a Signal Emitter listening to Transform events, or networking.
Axis locks
Section titled “Axis locks”The lock… props guard only the editor’s gizmo: a locked axis does not move whichever handle an author drags. Everything else still sets every axis: the setters, prop edits, scripts, undo, animation and networking. TransformComponent also has lockPosition(locked?), lockRotation(locked?) and lockScale(locked?), which lock (or, with false, unlock) all three axes of one part at once.
Events
Section titled “Events”| Event | EventType |
Data |
|---|---|---|
Transform::position- |
Event |
{ position }: the new local position. |
Transform::rotation- |
Event |
{ rotation }: the new local rotation, Euler radians. |
Transform::scale- |
Event |
{ scale }: the new local scale. |
Transform::change |
Event |
{ changed }: after the specific events, the parts this call changed (position, rotation, scale). |
They come from the setters and from prop edits, in play and in the editor. Writing entity.position directly, or physics moving the body, sends nothing.
Enable
Section titled “Enable”comp-enable turns the entity and everything under it on or off. It is required on every element and cannot itself be disabled. It is the one on/off switch a scene saves.
| Prop | Type | Default | Description |
|---|---|---|---|
enabled |
boolean | true |
Off hides and silences the entity’s subtree and stops its behaviour: no ticking, no physics, no audio, video or animation, and its scripts pause. |
From a script, use the entity shortcuts: entity.setEnabled(on) to switch, and entity.isEnabled() to ask whether the entity is effectively on (its own flag and every ancestor’s).
| Event | EventType |
Data |
|---|---|---|
Enable::change |
Event |
{ enabled }: the entity was enabled or disabled. |
comp-layer puts the entity, and what it builds or loads, on one layer. Layers group entities so they can be switched off, locked or made unclickable together. It is required on every element.
| Prop | Type | Default | Description |
|---|---|---|---|
layer |
integer | 0 |
A user layer, 0 to 20. Values 21 to 31 are reserved for the runtime and rejected. |
From a script, call entity.setLayer(n); read the current layer with entity.getComponent(LayerComponent).layer.
| Event | EventType |
Data |
|---|---|---|
Layer::change |
Event |
{ layer }: the entity moved to another layer. |
Layers explains what a layer controls.
comp-lock is an editor flag: a locked entity cannot be picked in the editor’s viewport or transformed there. It is not a run-time freeze: scripts and physics still move a locked entity. It is required on every element.
| Prop | Type | Default | Description |
|---|---|---|---|
locked |
boolean | false |
Blocks selection in the viewport and transforming in the editor. |
From a script, call entity.setLocked(on); read it with entity.getComponent(LockComponent).locked.
| Event | EventType |
Data |
|---|---|---|
Lock::change |
Event |
{ locked }: the entity was locked or unlocked. |
The floor-standing scenery is locked with Lock components, so in the editor it cannot be picked or moved by mistake, while scripts still move it. Clicking the crate locks or unlocks it and the Lock::change event reports it.
Read the log for the locked entities, then click the crate to lock or unlock it. The pillar is locked but the script still turns it. Download the project to see the locks in XR Creator. Drag to orbit the camera, scroll to zoom.
Billboard
Section titled “Billboard”comp-billboard keeps the entity turned toward the camera, for sprites, labels and name tags. The entity’s local +Z axis is aimed at the camera.
| Prop | Type | Default | Description |
|---|---|---|---|
lockY |
boolean | true |
Turn around the world Y axis only, so the entity stays upright. false faces the camera fully, tilting up and down too. |
On most entities, Billboard turns the entity itself, in play only, so the saved rotation never changes. On a UI panel it turns the panel’s drawn content instead, and also works in the editor. It sends no events.
comp-group is found only on Group entities (ent-group). It has no props. It recenters the group on demand: recenter() moves the group’s origin to the centre of its children’s bounding box, and every child stays where it is in the world. Nothing else triggers it: adding, removing or moving children leaves the origin where it is.
- A child counts by what it draws, everything under it included. A child that draws nothing, such as a light or an empty group, counts as a point at its position.
- Only positions change: the group’s rotation and scale, and its children’s, stay.
- Locked Transform axes do not stop it.
- It returns the entities it moved, the group first, or an empty list when there is no child entity or the group is already centred.
It sends no events.
Shadow
Section titled “Shadow”comp-shadow decides whether the entity’s meshes cast and receive shadows. Without it they do neither. Entity types choose their own starting values: a new primitive mesh casts but does not receive.
| Prop | Type | Default | Description |
|---|---|---|---|
cast |
boolean | true |
The meshes cast shadows. |
receive |
boolean | true |
The meshes receive shadows. |
It sends Shadow::change (EventType.Shadow.Change) with { changed }, only the settings that changed. Lights, shadow maps and shadow quality are covered in Lighting.
Don’t Unload
Section titled “Don’t Unload”comp-dont-unload has no props. It keeps the entity, and everything under it, when its scene is switched away in play: the entity moves to the new scene, still running. A script can do the same with #pragma dont_unload(). See Scene switching.
An amber companion orb has a Don't Unload component, so when you click a door and the project switches between the meadow and the cave scene, the orb, and the script on it, move with you while everything else is replaced.
Click the blue door to go to the cave, then the green door to come back: the orb stays with you and keeps counting the trips. Drag to orbit the camera, scroll to zoom.
Examples
Section titled “Examples”1. Move an object and hear its Transform events
Section titled “1. Move an object and hear its Transform events”This script gives a crate a Signal Emitter for its position changes, then moves it with a setter. Each move arrives back as an event.
#pragma lifecycle(startup)
function startup() { const crate = findEntity('Crate'); if (!crate) return;
crate.addComponent(new SignalEmitterComponent({ signal: 'crate/moved', eventFilter: ['Transform::position-change'], }));
on('crate/moved', (event) => { const p = event.data.position; log('Crate now at', p.x.toFixed(2), p.y.toFixed(2), p.z.toFixed(2)); });
crate.setPosition(1, 0.5, -2); // sends an event crate.position.x += 1; // a direct write: sends nothing}A script adds a filtered Signal Emitter to a crate, then moves it twice: setPosition reports a Transform event, a direct write to position does not.
Read the script log: one line for the setPosition call, none for the direct write.
How it works:
- The Signal Emitter forwards only
Transform::position-change, thanks to itseventFilter. setPositionsends the event, so the log line appears.- The direct write to
crate.position.xmoves the crate but sends nothing.
2. Name tags that always face the player
Section titled “2. Name tags that always face the player”This script gives every entity whose name ends in Tag a Billboard, so labels stay readable from anywhere.
#pragma lifecycle(startup)
function startup() { let count = 0; scene.entities.forEach((entity) => { if (!entity.name.endsWith('Tag')) return; if (entity.getComponent(BillboardComponent)) return; entity.addComponent(new BillboardComponent({ lockY: true })); count += 1; }); log('Billboards added:', count);}How it works:
scene.entitiesholds every entity of the scene, by id.- The check with
getComponentskips entities that already have a Billboard. lockY: truekeeps the tags upright; they only turn around the vertical axis.
3. Recenter a group built at run time
Section titled “3. Recenter a group built at run time”A script that spawns pieces under a group can recenter the group afterwards, so the group turns around the middle of its contents.
#pragma lifecycle(startup, update)
let pile = null;
function startup() { pile = findEntity('Pile'); const group = pile && pile.getComponent(GroupComponent); if (!group) { log('Pile is not a Group entity'); return; }
for (let i = 0; i < 4; i++) { spawn('Brick', { parent: pile, position: { x: 2 + i * 0.6, y: 0.25, z: 1 }, enabled: true }); }
const moved = group.recenter(); log('Recentered; entities moved:', moved.length);}
function update(dt) { if (pile) pile.rotation.y += dt * 0.5;}A script spawns four bricks under the Pile group, then calls recenter() on its Group component, so the pile spins around the middle of its bricks instead of its old origin.
Watch the bricks turn around their own centre; the log says how many entities recenter() moved. Drag to orbit the camera, scroll to zoom.
How it works:
spawncopies an entity namedBrickunder the group;enabled: trueswitches on copies of a disabled template.recenter()moves the group’s origin to the middle of the bricks without moving them in the world.- Turning
pile.rotation.ythen spins the bricks around their own centre, not around the old origin.
4. A light switch and a performance mode
Section titled “4. A light switch and a performance mode”A realistic script for a room: the E key switches a lamp on and off, and on phones and tablets shadows are turned off to save work.
#pragma lifecycle(startup, update)
let lamp = null;
function reduceShadows() { let changed = 0; scene.entities.forEach((entity) => { const shadow = entity.getComponent(ShadowComponent); if (!shadow) return; shadow.setProps({ cast: false, receive: false }); changed += 1; }); log('Shadows off on', changed, 'entities');}
function startup() { lamp = findEntity('Desk Lamp'); if (!lamp) log('No entity named Desk Lamp'); if (Device.isMobile()) reduceShadows();}
function update() { if (lamp && Input.keyboard.isKeyReleased(Key.E)) { lamp.setEnabled(!lamp.isEnabled()); log('Lamp is', lamp.isEnabled() ? 'on' : 'off'); }}How it works:
reduceShadowswalks every entity once and switches its Shadow off, only on mobile devices (Device.isMobile()).isKeyReleasedis true for one frame per key press, so each press toggles exactly once.setEnabledchanges the lamp’s Enable component;isEnabled()reads the result back.- The script keeps working while the lamp is off because it sits on another entity. A script under the lamp would pause with it.
Common mistakes
Section titled “Common mistakes”Writing position and expecting physics to follow
Section titled “Writing position and expecting physics to follow”A direct write moves what is drawn, but the colliders stay where they were and no event is sent.
crate.position.set(0, 3, 0); // the physics body is left behindcrate.setPosition(0, 3, 0);Expecting Lock or axis locks to stop a script
Section titled “Expecting Lock or axis locks to stop a script”Lock and the Transform lock… props protect an entity from the editor’s tools only. In play, scripts, physics and animation move it freely. To keep an entity still in play, simply do not move it, or switch off what moves it.
Disabling a core component
Section titled “Disabling a core component”Transform, Enable, Layer and Lock are always enabled; other required components can be disabled. To switch the entity off, use its own switch.
door.getComponent(EnableComponent).setEnabled(false);door.setEnabled(false);Using a reserved layer
Section titled “Using a reserved layer”Layers 21 to 31 belong to the runtime; a Layer component rejects them with an error.
marker.setLayer(25);marker.setLayer(5);