Skip to content

Script cleanup uses the class name instead of the registered scriptName #343

Description

@ice1080

Description

useScript creates scripts using their constructor but destroys them using a name derived from scriptConstructor.name:

const scriptName = toLowerCamelCase(scriptConstructor.name);

This can differ from the name PlayCanvas uses to register the script when the constructor defines a static scriptName.

Bundlers can expose this mismatch. In my Vite development build, OrbitCamera becomes _OrbitCamera, while its static scriptName remains orbitCamera. Cleanup therefore calls destroy('_OrbitCamera'), which fails to remove the registered script.

Steps to reproduce

  1. Mount <OrbitControls /> on a camera in a Vite application.
  2. Conditionally unmount the controls while keeping the camera entity alive.
  3. Mount another camera controller on the same camera.

In my application, the globe orbit controls are unmounted before a separate part of the scene takes over the shared camera. But because the orbit script survives unmounting, the orbit controller continues writing the camera transform, competing with the new controller.

Remounting also produces warnings such as:

script 'orbitCamera' is already added to entity 'camera'

React StrictMode's effect cleanup/remount cycle exposes these duplicate-script warnings as well.

Environment

  • @playcanvas/react: 0.11.5
  • PlayCanvas: 2.21.3
  • React: 19
  • Vite: 6
  • Desktop Chrome

Proposed fix

Prefer the constructor's explicit scriptName, retaining the existing class-name fallback:

const scriptName = scriptConstructor.scriptName || toLowerCamelCase(scriptConstructor.name);

I verified the mismatch with a PlayCanvas script whose class name is _OrbitCamera and whose static scriptName is orbitCamera: destroying _OrbitCamera returns false and leaves the script attached; destroying orbitCamera succeeds.

Found with the assistance of GPT-6 Astra, but verified manually.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions