{"record":{"id":"74d4b73548ed3b09","repo":"stride3d/stride","slug":"both-rigidbodies-must-be-valid","errorCode":null,"errorMessage":"Both RigidBodies must be valid","messagePattern":"Both RigidBodies must be valid","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Physics/Simulation.cs","lineNumber":649,"sourceCode":"        \n        /// <summary>\n        /// Creates the constraint.\n        /// </summary>\n        /// <param name=\"type\">The type.</param>\n        /// <param name=\"rigidBodyA\">The rigid body a.</param>\n        /// <param name=\"rigidBodyB\">The rigid body b.</param>\n        /// <param name=\"frameA\">The frame a.</param>\n        /// <param name=\"frameB\">The frame b.</param>\n        /// <param name=\"useReferenceFrameA\">if set to <c>true</c> [use reference frame a].</param>\n        /// <returns></returns>\n        /// <exception cref=\"System.Exception\">\n        /// Cannot perform this action when the physics engine is set to CollisionsOnly\n        /// or\n        /// Both RigidBodies must be valid\n        /// </exception>\n        public static Constraint CreateConstraint(ConstraintTypes type, RigidbodyComponent rigidBodyA, RigidbodyComponent rigidBodyB, Matrix frameA, Matrix frameB, bool useReferenceFrameA = false)\n        {\n            if (rigidBodyA == null || rigidBodyB == null) throw new Exception(\"Both RigidBodies must be valid\");\n            return CreateConstraintInternal(type, rigidBodyA, frameA, rigidBodyB, frameB, useReferenceFrameA);\n        }\n\n        /// <summary>\n        /// Creates a hinge constraint using a specialized constructor.\n        /// </summary>\n        /// <param name=\"rigidBodyA\">The rigid body a.</param>\n        /// <param name=\"pivotInA\">Pivot point in body a.</param>\n        /// <param name=\"axisInA\">Axis in body a.</param>\n        /// <param name=\"useReferenceFrameA\">if set to <c>true</c> [use reference frame a].</param>\n        /// <exception cref=\"System.Exception\">\n        /// Cannot perform this action when the physics engine is set to CollisionsOnly\n        /// or\n        /// RigidBody must be valid\n        /// </exception>\n        public static HingeConstraint CreateHingeConstraint(RigidbodyComponent rigidBodyA, Vector3 pivotInA, Vector3 axisInA, bool useReferenceFrameA = false)\n        {\n            if (rigidBodyA == null) throw new Exception(\"RigidBody must be valid\");","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Physics/Simulation.cs#L631-L667","documentation":"CreateConstraint builds a physics constraint between two rigid bodies; both bodies are mandatory inputs used to create the native Bullet constraint. Passing null for either body would produce a native crash, so the public API validates and throws before reaching BulletSharp.","triggerScenarios":"Calling Simulation.CreateConstraint(type, rigidBodyA, rigidBodyB, frameA, frameB) with rigidBodyA or rigidBodyB set to null (including via default/omitted arguments upstream).","commonSituations":"Resolving body references via late-bound lookups (Get<T> on entities) that return null; wiring constraints before the rigid body components are initialized.","solutions":["Obtain valid RigidbodyComponent references for both bodies before calling CreateConstraint.","Ensure the referenced entities/components are loaded and enabled (non-null) at constraint creation time.","Defer constraint creation until after scene/entity initialization completes."],"exampleFix":"// before\nvar c = Simulation.CreateConstraint(ConstraintTypes.Point2Point, bodyA, bodyB, frameA, frameB); // bodyB null\n// after\nif (bodyA == null || bodyB == null)\n    throw new InvalidOperationException(\"Constraint requires both rigid bodies to be resolved\");\nvar c = Simulation.CreateConstraint(ConstraintTypes.Point2Point, bodyA, bodyB, frameA, frameB);","handlingStrategy":"validation","validationCode":"if (rigidBodyA == null || rigidBodyB == null) throw new InvalidOperationException(\"CreateConstraint requires both bodies non-null\");","typeGuard":"bool CanConstrain(RigidbodyComponent a, RigidbodyComponent b) => a != null && b != null;","tryCatchPattern":"try { var c = Simulation.CreateConstraint(t, a, b, fa, fb); } catch (Exception ex) when (ex.Message == \"Both RigidBodies must be valid\") { /* resolve body refs and retry setup */ }","preventionTips":["Resolve RigidbodyComponent references in Start before building constraints.","Log null component lookups immediately instead of propagating null.","Use non-null assertions at entity wiring time."],"tags":["physics","stride","null-reference","constraint"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}