{"record":{"id":"fd796034bcfc8c3d","repo":"BabylonJS/Babylon.js","slug":"missing-scene-parameter-for-constraint-constructor","errorCode":null,"errorMessage":"Missing scene parameter for constraint constructor.","messagePattern":"Missing scene parameter for constraint constructor\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/core/src/Physics/v2/physicsConstraint.ts","lineNumber":47,"sourceCode":"    protected _type: PhysicsConstraintType;\r\n    /**\r\n     * @internal\r\n     * The internal options that were used to init the constraint\r\n     */\r\n    public _initOptions?: PhysicsConstraintParameters;\r\n\r\n    /**\r\n     * Constructs a new constraint for the physics constraint.\r\n     * @param type The type of constraint to create.\r\n     * @param options The options for the constraint.\r\n     * @param scene The scene the constraint belongs to.\r\n     *\r\n     * This code is useful for creating a new constraint for the physics engine. It checks if the scene has a physics engine, and if the plugin version is correct.\r\n     * If all checks pass, it initializes the constraint with the given type and options.\r\n     */\r\n    constructor(type: PhysicsConstraintType, options: PhysicsConstraintParameters, scene: Scene) {\r\n        if (!scene) {\r\n            throw new Error(\"Missing scene parameter for constraint constructor.\");\r\n        }\r\n        const physicsEngine = scene.getPhysicsEngine();\r\n        if (!physicsEngine) {\r\n            throw new Error(\"No Physics Engine available.\");\r\n        }\r\n        if (physicsEngine.getPluginVersion() != 2) {\r\n            throw new Error(\"Plugin version is incorrect. Expected version 2.\");\r\n        }\r\n        const physicsPlugin = physicsEngine.getPhysicsPlugin();\r\n        if (!physicsPlugin) {\r\n            throw new Error(\"No Physics Plugin available.\");\r\n        }\r\n\r\n        this._physicsPlugin = physicsPlugin as IPhysicsEnginePluginV2;\r\n        this._options = options;\r\n        this._type = type;\r\n    }\r\n\r","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/core/src/Physics/v2/physicsConstraint.ts#L29-L65","documentation":"PhysicsConstraint's constructor requires a Scene to look up the physics engine and plugin version before creating the constraint. Passing null/undefined for scene trips the first guard and throws immediately, since without a scene no engine or plugin can be resolved.","triggerScenarios":"new PhysicsConstraint(PhysicsConstraintType.BALL_AND_SOCKET, params, undefined) — omitting the third scene argument, or passing a variable that is null because the scene was fetched too early (e.g. engine createScene not awaited).","commonSituations":"Copy-pasted constructor calls missing the scene argument; scene obtained from a factory returning a promise that was not awaited; refactoring code where the scene variable was renamed/shadowed to null.","solutions":["Pass the live Scene as the third argument: new PhysicsConstraint(type, params, scene).","Ensure the scene variable is fully initialized (await engine.createSceneAsync() if async) before constructing the constraint.","Add an if (!scene) guard or assertion in your setup code before creating any constraints."],"exampleFix":"// before\nconst constraint = new PhysicsConstraint(PhysicsConstraintType.BALL_AND_SOCKET, params, undefined);\n// after\nconst constraint = new PhysicsConstraint(PhysicsConstraintType.BALL_AND_SOCKET, params, scene);","handlingStrategy":"validation","validationCode":"if (!(scene instanceof Scene)) {\n  throw new Error(\"PhysicsConstraint requires a live Scene as its third argument\");\n}","typeGuard":"function hasScene(s: unknown): s is Scene {\n  return s instanceof Scene;\n}","tryCatchPattern":"try {\n  const constraint = new PhysicsConstraint(PhysicsConstraintType.BALL_AND_SOCKET, params, scene);\n} catch (e) {\n  if ((e as Error).message.includes(\"Missing scene parameter\")) {\n    console.error(\"Pass the live Scene instance as the third constructor argument\", e);\n  }\n  throw e;\n}","preventionTips":["Always pass the scene as the third argument to the PhysicsConstraint constructor.","Ensure the scene is created/awaited before constraint construction.","Type your factory helpers to take scene: Scene (non-optional) so omission is a compile error."],"tags":["physics","constraint","missing-argument","babylon"],"backgroundTag":"missing-required-argument","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}