XINCGer/Unity3DTraining · error · KeyNotFoundException

No such field number

Error message

No such field number

What it means

MessageDescriptor.Fields indexer by number looks up the field via FindFieldByNumber and throws KeyNotFoundException with 'No such field number' when no field with that number exists in the message. It is the dictionary-style contract of the FieldCollection indexer.

Solutions

  1. Use FindFieldByNumber(number) and null-check instead of the throwing indexer when the field may not exist.
  2. Verify the field number against the current .proto / generated code for the message.
  3. Ensure client and server use matching schema versions so the expected field number exists.
  4. Check that you're inspecting the correct MessageDescriptor (right message type).

Example fix

// before
var field = descriptor.Fields[42]; // KeyNotFoundException if absent
// after
var field = descriptor.FindFieldByNumber(42);
if (field != null) { /* use field */ }
Defensive patterns

Strategy: validation

Validate before calling

var field = descriptor.FindFieldByNumber(number); if (field == null) { /* handle missing field */ }

Type guard

bool HasFieldNumber(MessageDescriptor d, int n) => d.FindFieldByNumber(n) != null;

Try / catch

try { var f = descriptor.Fields[number]; } catch (KeyNotFoundException) { field = null; /* unknown field — skip or fall back */ }

Prevention

When it happens

Trigger: Accessing descriptor.Fields[number] where the number is not defined in the message — e.g. reading a field number from an unknown/wire-decoded context, or assuming a field exists across schema versions.

Common situations: Schema drift: code referencing a field number from an older/newer .proto version; iterating extension or unknown field numbers and probing Fields; hand-written reflection code with off-by-one or wrong field numbers.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of XINCGer/Unity3DTraining@016f98412e (2026-09-12). Data as JSON: /api/errors/16771bc2c0788c3f. Report an issue: GitHub.

Appendix: source

Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/MessageDescriptor.cs:324

            {
                return messageDescriptor.jsonFieldMap;
            }

            /// <summary>
            /// Retrieves the descriptor for the field with the given number.
            /// </summary>
            /// <param name="number">Number of the field to retrieve the descriptor for</param>
            /// <returns>The accessor for the given field</returns>
            /// <exception cref="KeyNotFoundException">The message descriptor does not contain a field
            /// with the given number</exception>
            public FieldDescriptor this[int number]
            {
                get
                {
                    var fieldDescriptor = messageDescriptor.FindFieldByNumber(number);
                    if (fieldDescriptor == null)
                    {
                        throw new KeyNotFoundException("No such field number");
                    }
                    return fieldDescriptor;
                }
            }

            /// <summary>
            /// Retrieves the descriptor for the field with the given name.
            /// </summary>
            /// <param name="name">Name of the field to retrieve the descriptor for</param>
            /// <returns>The descriptor for the given field</returns>
            /// <exception cref="KeyNotFoundException">The message descriptor does not contain a field
            /// with the given name</exception>
            public FieldDescriptor this[string name]
            {
                get
                {
                    var fieldDescriptor = messageDescriptor.FindFieldByName(name);
                    if (fieldDescriptor == null)

View on GitHub (pinned to 016f98412e)