Group
Updated
- Type
ent-group- Script class
GroupEntity- Family
- Scene
Overview
Section titled “Overview”A Group is an empty entity that holds other entities. It draws nothing itself; it has only the components every placed entity has: Layer, Lock, Enable and a Transform. Its job is to organise the hierarchy and to act on its children together:
- Move, turn and scale together. Children follow their parent’s transform, so moving the group moves everything under it.
- Switch together. Disabling the group hides its children and stops their behaviour, scripts included.
- Copy together.
spawncopies an entity with its whole subtree, so a group makes a reusable template.
Use a Group whenever several objects belong together: the parts of a machine, the furniture of a room, the content of one level, the world behind a portal. Pick a Behavior entity instead when you need a place for a script, and the Scene Root for settings that apply to the whole scene.
A Group starts with no props of its own. You can add many optional components to it, for example a rigid body and colliders to make the group one physics object, a Grabbable to pick it up, or a Group component (comp-group), whose recenter() moves the group’s origin to the centre of its children without moving them in the world.
Set it up
Section titled “Set it up”- Add a Group entity to the scene (see Scenes and hierarchy) and give it a clear name.
- Place it where the set of objects should pivot: rotating and scaling happen around the group’s origin.
- Drag the entities that belong together under it in the hierarchy.
- Add optional components to the group itself when the set should behave as one, for example a Grabbable.
Examples
Section titled “Examples”Turn a display stand
Section titled “Turn a display stand”Goal: a group of objects on a stand turns slowly around its own origin.
#pragma lifecycle(startup, update)
const SPEED = 0.5; // radians per secondlet stand;
function startup() { stand = findEntity('Display Stand');}
function update(dt) { stand.rotation.y += SPEED * dt;}findEntity('Display Stand')finds the Group by name, once, instartup.- Turning the group’s
rotation.yturns every child with it, around the group’s origin. dtis the time since the last frame in seconds, so the speed does not depend on the frame rate.
Show one room at a time
Section titled “Show one room at a time”Goal: pressing 1, 2 or 3 shows one room and hides the others.
Each room is a Group: Room 1, Room 2, Room 3.
#pragma lifecycle(startup, update)
const ROOMS = ['Room 1', 'Room 2', 'Room 3'];const KEYS = [Key.Digit1, Key.Digit2, Key.Digit3];let rooms = [];
function show(index) { rooms.forEach((room, i) => room.setEnabled(i === index));}
function startup() { rooms = ROOMS.map((name) => findEntity(name)).filter(Boolean); show(0);}
function update() { KEYS.forEach((key, i) => { if (Input.keyboard.isKeyReleased(key)) show(i); });}entity.setEnabled(false)hides the group and everything under it, and pauses the scripts inside it.filter(Boolean)drops names that are not in the scene.- Only one room is enabled at a time, so hidden rooms cost nothing to draw.
Recenter a group after building it
Section titled “Recenter a group after building it”Goal: after a script fills a group with spawned crates, move the group’s origin to the middle of the pile so it turns around its centre.
#pragma lifecycle(startup)
function startup() { const pile = findEntity('Crate Pile');
for (let i = 0; i < 5; i++) { spawn('Crate Template', { parent: pile, position: { x: i * 1.1, y: 0, z: 0 }, enabled: true, }); }
const group = pile.getComponent(GroupComponent) ?? pile.addComponent(new GroupComponent()).getComponent(GroupComponent); const moved = group.recenter(); log('Recentered', moved.length, 'entities');
Tween.to(pile.rotation, { y: M_TAU }, { duration: 6, repeat: Infinity });}spawn('Crate Template', { parent: pile, … })copies a disabled template crate under the group, at a local position, and enables the copy.- A Group does not start with a Group component, so the script adds one when it is missing.
addComponentreturns the entity. recenter()moves the group’s origin to the centre of its children’s bounding box and keeps every child where it is in the world. It returns the entities it moved, or none when there is nothing to do.Tween.to(pile.rotation, { y: M_TAU }, …)turns the pile a full turn every six seconds, now around its centre.
- A Group component never recenters on its own: adding, removing or moving children changes nothing until you call
recenter(). - Look groups up once in
startupand keep the entity; walking the scene by name every frame is wasted work.
Components
Section titled “Components”What a new Group is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
You can also add 55 optional components, such as Animation Graph, Animation Timeline, Billboard, Box Collider, Capsule Collider, Character Controller.
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
OpenCV.js, imported from a CDN, finds the edges in a picture drawn on a script canvas with Canny edge detection, and two planes show the picture and its edges side by side.
OpenCV.js is an 11 MB download (2.8 MB compressed): wait a few seconds for the edges to appear. Then click the scene and press Space to try other thresholds. Drag to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Group.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Group'); if (!(entity instanceof GroupEntity)) return; log(entity.name, entity.position);}