{"record":{"id":"1cfb754acbce750f","repo":"Unity-Technologies/ml-agents","slug":"index-out-of-bounds-expected-a-number-between-0-a","errorCode":null,"errorMessage":"Index out of bounds, expected a number between 0 and {Length}","messagePattern":"Index out of bounds, expected a number between 0 and (.+?)","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"com.unity.ml-agents/Runtime/Actuators/ActionSegment.cs","lineNumber":88,"sourceCode":"\n        /// <summary>\n        /// Get the underlying <see cref=\"Array\"/> of this segment.\n        /// </summary>\n        public T[] Array { get; }\n\n        /// <summary>\n        /// Allows access to the underlying array using array syntax.\n        /// </summary>\n        /// <param name=\"index\">The zero-based index of the segment.</param>\n        /// <exception cref=\"IndexOutOfRangeException\">Thrown when the index is less than 0 or\n        /// greater than or equal to <see cref=\"Length\"/></exception>\n        public T this[int index]\n        {\n            get\n            {\n                if (index < 0 || index > Length)\n                {\n                    throw new IndexOutOfRangeException($\"Index out of bounds, expected a number between 0 and {Length}\");\n                }\n                return Array[Offset + index];\n            }\n            set\n            {\n                if (index < 0 || index > Length)\n                {\n                    throw new IndexOutOfRangeException($\"Index out of bounds, expected a number between 0 and {Length}\");\n                }\n                Array[Offset + index] = value;\n            }\n        }\n\n        /// <summary>\n        /// Sets the segment of the backing array to all zeros.\n        /// </summary>\n        public void Clear()\n        {","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/com.unity.ml-agents/Runtime/Actuators/ActionSegment.cs#L70-L106","documentation":"ActionSegment<T>'s read indexer validates that the index lies between 0 and Length (inclusive upper bound is actually allowed by the check but the message says 'between 0 and Length') before reading Array[Offset + index]. An out-of-range index means the caller is trying to read more actions than the behavior's action space provides. This protects against silently reading another segment's data when Offset indexing goes out of bounds.","triggerScenarios":"Accessing actions[i] (via an ActionSegment from ActionBuffers.ContinuousActions/DiscreteActions) where i < 0 or i exceeds the segment Length, e.g. reading index 5 of a 3-action continuous segment.","commonSituations":"Agent action-reaction code hardcoding action counts that no longer match the Behavior Parameters / ActionSpec after editing the number of actions in the trainer config; mismatched branch counts between continuous and discrete settings.","solutions":["Update the agent code to use actions.Length or ActionSpec instead of a hardcoded action count","Align the Behavior Parameters on the agent prefab with the trainer YAML hyperparameters (action_size / action branches)","For hybrid spaces, read the correct segment: ContinuousActions or DiscreteActions, not both"],"exampleFix":"// before\nvar turn = actions[5];\n// after\nif (actions.Length > 5) { var turn = actions[5]; }","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < actions.Length) {\n    var value = actions[index];\n}","typeGuard":"bool IsValidActionIndex(ActionSegment<float> a, int i) => i >= 0 && i < a.Length;","tryCatchPattern":"try { var v = actions[index]; } catch (IndexOutOfRangeException) { /* use default action */ }","preventionTips":["Never hardcode action indices; derive counts from ActionSpec","Keep Behavior Parameters in sync with trainer config","Iterate with for (int i = 0; i < actions.Length; i++)"],"tags":["unity","ml-agents","index-out-of-range","actions"],"backgroundTag":"index-out-of-bounds","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}