Skip to main content

<Script/>

The <Script/> component provides a simple imperative way to add behaviors to an <Entity/>.

This is useful for things like physics, animation, and interactivity where, for performance reasons, you want to bypass updating React props. It allows you to hook into the engine's update loop outside of React, which is useful for performance-critical code. You can also leverage existing Scripts from PlayCanvas Editor Projects.

You can find more information about Scripts in the Engine documentation.

Usage

The Script component takes a class that extends the PlayCanvas Script class and attaches it to the Entity. Any additional props are passed to the Script class directly and can be used as properties on the class.

import { Script } from '@playcanvas/react/components'
import { CameraControls } from 'playcanvas/scripts/esm/camera-controls.mjs'

return <Script script={CameraControls} />

The following examples adds a SpinMe script to the entity that rotates the entity every frame.

You can use any existing Script or create your own, or any from the engine repository for existing Scripts.

tip

The PlayCanvas engine contains a number of useful scripts that you can use in your projects. Find them here.

You can learn more about the anatomy of a ESM Script in the Scripting documentation.

Properties

NameTypeDefault
script
SubclassOf<Script>
The Script class to attach to the entity
-