# ScriptInstanceElement

Class · extends [`AsyncElement`](https://api.playcanvas.com/web-components/classes/AsyncElement.md) · category: Components

Source: https://github.com/playcanvas/web-components/blob/252f001881302c266c8d727044408c32be2bb5e7/src/components/script-instance.ts#L50

The ScriptInstanceElement interface provides properties and methods for manipulating
`<pc-script-instance>` elements. The ScriptInstanceElement interface also inherits the properties and
methods of the [AsyncElement](https://api.playcanvas.com/web-components/classes/AsyncElement.md) interface.

Script attributes can be supplied through two channels:

- **Per-property attributes**: any non-reserved attribute on the element maps to the script
  attribute of the same name (kebab-case to camelCase, e.g. `focus-point` → `focusPoint`).
  Values are parsed according to the type of the attribute's current value — initially the
  script's declared default (numbers, booleans, strings, Vec2/3/4, Color, Quat as Euler
  angles) — and the `asset:`/`entity:`/`vec2:`/`vec3:`/`vec4:`/`color:` prefixes may be used
  to be explicit. An `entity:` reference is an entity name — resolved against the nearest
  enclosing entity first, then outward, then the document — or a document-wide `#` selector
  (`entity:#id`); a bare value is always a name, never an element id.
- **The `attributes` JSON attribute**: an object supporting nested structures and attribute
  names that collide with reserved HTML attribute names (e.g. `title`).

When both specify the same attribute, the per-property attribute wins — at creation and
whenever either channel changes at runtime. The element's own `name` and `enabled`
attributes configure the element itself and are not script attributes.

Changing `name` on a live element destroys the old-name script instance and creates the
new-name one, re-applying both attribute channels to it.

The element becomes ready once its script instance has been created by the parent
`<pc-script>` element. A script class registered after the element is waited for, and its
instance created with the element's declared state when it arrives; one still missing once no
script asset is left loading logs a warning.

**elementSummary** The `<pc-script-instance>` element attaches one script class, named by `name`, to
the entity of its parent `<pc-script>`. Its other attributes set script attributes of the same
name, and `attributes` takes a JSON object instead. An `entity:` value is an entity name —
write `entity:#id` for an element id. Must be a direct child of `<pc-script>`.

**fires** scriptattributeschange - Fired when the script's attributes change. The
`detail` carries the new `attributes` object. Bubbles.

**fires** scriptenablechange - Fired when the script's enabled state changes. The
`detail` carries the new `enabled` state. Bubbles.

**fires** scriptnamechange - Fired when the script is renamed on a live element. The
`detail` carries `oldName` and `newName`. Bubbles.

## Accessors

### enabled

```ts
get enabled(): boolean
set enabled(value: boolean)
```

Gets the enabled state of the script.

### name

```ts
get name(): string
set name(value: string)
```

Gets the name of the script.

### script

```ts
get script(): Script | null
```

Gets the [Script](https://api.playcanvas.com/engine/classes/Script.html) instance created for this element. Returns `null` until the
instance exists — await [whenReady](https://api.playcanvas.com/web-components/functions/whenReady.md) or the element's `ready()` promise before
accessing it.

### scriptAttributes

```ts
get scriptAttributes(): Record<string, any>
set scriptAttributes(value: Record<string, any>)
```

Gets the attributes of the script as an object whose `asset:`, `entity:`, `vec2:`, `vec3:`,
`vec4:` and `color:` prefixed values are resolved when applied — an `entity:` value being
an entity name (nearest enclosing entity first, then outward, then the document) or a
document-wide `#` selector (`entity:#id`), never a bare element id.

## Inherited from [AsyncElement](https://api.playcanvas.com/web-components/classes/AsyncElement.md)

- `get closestApp(): AppElement | null`
- `get closestEntity(): EntityBaseElement | null`
- `protected _onReady(): void`
- `protected _resetReady(): void`
- `ready(): Promise<ScriptInstanceElement>`
