Skip to content

Behaviour Lifecycle & Events ​

Behaviour is dispatched by the ScriptBehaviourSystem each frame. Knowing the precise order of hooks helps you place state setup, simulation, and cleanup correctly.

Frame order ​

World.update(dt)
  ├── ObjectManagementSystem        # parent reconciliation, entity lifecycle
  ├── (geometry / material systems)
  ├── (custom user systems)
  ├── ScriptBehaviourSystem
  │     ├── for each enabled script:
  │     │     ├── if first run → init()
  │     │     └── onUpdate(), or onPausedUpdate() while paused
  ├── (sync systems)
  ├── AnimationSystem, BoneHoldSystem, IKSystem
  ├── TransformSyncSystem
  ├── ScriptLateUpdateSystem
  │     └── onLateUpdate()
  ├── CameraRigSystem               # Camera Rigs and cutscenes
  └── RenderSystem

This ordering means:

  • this.transform mutations in onUpdate() are picked up the same frame by TransformSyncSystem and applied to the three.js object.
  • Animation runs after scripts, so animating a value will overwrite a script's mutation on the same property.
  • onLateUpdate() runs once everything has moved and reached three.js, which is where a camera belongs. See Camera Rigs.

Hook reference ​

HookOverride?FrequencyRuns in editor?Purpose
init()Yes (protected)Once before first onUpdateNoDefault and validate data, cache references
onUpdate()Yes (protected)Every frame, unless pausedNoPer-frame logic
onLateUpdate()Yes (protected)Every frame, after everything has moved, unless pausedNoCameras, and anything that follows something else
onPausedUpdate()Yes (protected)Every frame while paused, instead of onUpdate()NoPause-menu logic
onPause() / onResume()OptionalWhen the game pauses and resumesNoReact to a pause
dispose()YesOnce on detach/destroyNoRelease timers, listeners, resources
onSelect() / onDeselect()OptionalEditor selection togglesYesUX-only hooks
onTargetFound() / onTargetLost()OptionalImage trackingNoReact to AR target visibility

Behaviours never run in the editor. For a visual that must render and animate in the editor, use a Script Object.

While the game is paused ​

world.pause() swaps onUpdate() for onPausedUpdate() until world.resume():

onUpdate()         # running
onPause()          # once, from inside world.pause()
onPausedUpdate()   # every frame, with this.deltaTime valid
onPausedUpdate()
onResume()         # once, from inside world.resume()
onUpdate()         # running again

onPause and onResume reach every script, hidden entities included. onPausedUpdate skips hidden entities, as onUpdate does.

A pause or resume takes effect at once, but a script that hasn't run yet that frame gets neither per-frame hook until the next one. That is why one key press can't both open and close a menu.

Other hooks may stop too: onSelect, onSelectStart, onGrab and hand poses don't fire while paused, and UI and multiplayer hooks do. See Pausing the Game.

When a hook throws ​

The engine catches the error, so one broken script never stops the frame or other scripts. What happens next depends on the hook:

  • Load, init() or onUpdate(): the engine disposes the instance, sets the entry's runtimeState to "error" with the message in runtimeMessage, and waits about one second before it loads and initialises the script again. A script that throws on every frame therefore restarts about once a second, not 60 times.
  • Event hooks (onSelect, onUiPress, onGrab, onXRSessionStart, onPause, onPausedUpdate and the rest): the instance keeps running and the next event calls the hook again.

Either way the engine logs the first occurrence of each distinct error with console.error, naming the entity, class and phase. Subscribe with world.onScriptError to receive the same reports in code.