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
- Mount
<OrbitControls /> on a camera in a Vite application.
- Conditionally unmount the controls while keeping the camera entity alive.
- 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.
Description
useScriptcreates scripts using their constructor but destroys them using a name derived fromscriptConstructor.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,
OrbitCamerabecomes_OrbitCamera, while its staticscriptNameremainsorbitCamera. Cleanup therefore callsdestroy('_OrbitCamera'), which fails to remove the registered script.Steps to reproduce
<OrbitControls />on a camera in a Vite application.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
Proposed fix
Prefer the constructor's explicit
scriptName, retaining the existing class-name fallback:I verified the mismatch with a PlayCanvas script whose class name is
_OrbitCameraand whose staticscriptNameisorbitCamera: destroying_OrbitCamerareturns false and leaves the script attached; destroyingorbitCamerasucceeds.Found with the assistance of GPT-6 Astra, but verified manually.