{"record":{"id":"f4dac6ba1d08708e","repo":"stride3d/stride","slug":"nameof-rigidbodya-must-be-valid","errorCode":null,"errorMessage":"{nameof(rigidBodyA)} must be valid","messagePattern":"(.+?) must be valid","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Physics/Simulation.cs","lineNumber":695,"sourceCode":"        /// <param name=\"rigidBodyB\">The rigid body b.</param>\n        /// <param name=\"pivotInB\">Pivot point in body b.</param>\n        /// <param name=\"axisInB\">Axis in body b.</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        /// Both RigidBodies must be valid\n        /// </exception>\n        public static HingeConstraint CreateHingeConstraint(RigidbodyComponent rigidBodyA, Vector3 pivotInA, Vector3 axisInA, RigidbodyComponent rigidBodyB, Vector3 pivotInB, Vector3 axisInB, bool useReferenceFrameA = false)\n        {\n            if (rigidBodyA == null || rigidBodyB == null) throw new Exception(\"Both RigidBodies must be valid\");\n            return CreateHingeConstraintInternal(rigidBodyA, rigidBodyB, pivotInA, pivotInB, axisInA, axisInB, useReferenceFrameA);\n        }\n\n\n        static Constraint CreateConstraintInternal(ConstraintTypes type, RigidbodyComponent rigidBodyA, Matrix frameA, RigidbodyComponent rigidBodyB = null, Matrix frameB = default, bool useReferenceFrameA = false)\n        {\n            if (rigidBodyA == null) throw new Exception($\"{nameof(rigidBodyA)} must be valid\");\n            if (rigidBodyB != null && rigidBodyB.Simulation != rigidBodyA.Simulation) throw new Exception(\"Both RigidBodies must be on the same simulation\");\n\n            Constraint constraintBase;\n            var rbA = rigidBodyA.InternalRigidBody;\n            var rbB = rigidBodyB?.InternalRigidBody;\n            switch (type)\n            {\n                case ConstraintTypes.Point2Point:\n                {\n                    var constraint = new Point2PointConstraint\n                    {\n                        InternalPoint2PointConstraint = \n                            rigidBodyB == null ? \n                            new BulletSharp.Point2PointConstraint(rbA, frameA.TranslationVector ) :\n                            new BulletSharp.Point2PointConstraint(rbA, rbB, frameA.TranslationVector, frameB.TranslationVector),\n                    };\n                    constraintBase = constraint;\n","sourceCodeStart":677,"sourceCodeEnd":713,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Physics/Simulation.cs#L677-L713","documentation":"CreateConstraintInternal is the shared private path for all constraint factories. It requires a non-null rigidBodyA (the primary body) and throws with the message \"rigidBodyA must be valid\" (interpolated via nameof). This is an internal invariant enforced on the common path used by CreateConstraint and both hinge overloads.","triggerScenarios":"Any constraint factory invoked with rigidBodyA == null — e.g. Simulation.CreateConstraint(type, null, bodyB, ...) or CreateHingeConstraint(null, ...) reaching the internal path.","commonSituations":"Generic helper code that passes whichever body variable happens to be first; editor field left unassigned; Get<T> lookups returning null on wrong entities.","solutions":["Resolve and validate rigidBodyA before calling any CreateConstraint/CreateHingeConstraint API.","Check entity assignment in the editor / component lookups so the primary body is present.","Wrap constraint setup in a guard that logs which body reference is missing."],"exampleFix":"// before\nSimulation.CreateConstraint(ConstraintTypes.Hinge, rigidBodyA, rigidBodyB, frameA, frameB); // rigidBodyA null\n// after\nvar rbA = Entity.GetComponent<RigidbodyComponent>() ?? throw new InvalidOperationException(\"rigidBodyA must be valid\");\nSimulation.CreateConstraint(ConstraintTypes.Hinge, rbA, rigidBodyB, frameA, frameB);","handlingStrategy":"type-guard","validationCode":"if (rigidBodyA == null) throw new InvalidOperationException(\"rigidBodyA must be valid\");","typeGuard":"RigidbodyComponent RequireBody(RigidbodyComponent b) => b ?? throw new InvalidOperationException(\"rigidBodyA must be valid\");","tryCatchPattern":"try { CreateConstraints(...); } catch (Exception ex) when (ex.Message.Contains(\"rigidBodyA must be valid\")) { /* re-resolve primary body */ }","preventionTips":["Make the primary body an explicit, validated constructor/script parameter.","Fail at component init when the primary body is missing.","Don't forward possibly-null bodies into generic constraint helpers."],"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"}