XINCGer/Unity3DTraining · error · KeyNotFoundException
No such field name
Error message
No such field name
What it means
MessageDescriptor's FieldCollection indexer (accessed via descriptor.Fields[name]) throws KeyNotFoundException when the field name does not exist in the message's protobuf schema. Google.Protobuf uses this instead of returning null so callers fail fast on typos. The name lookup is case-sensitive against the .proto field names.
Solutions
- Check the field exists first: if (desc.Fields.ByJsonName(name) != null) or wrap the indexer access in try/catch (KeyNotFoundException).
- Verify the exact .proto field name (snake_case) vs the JSON name (camelCase) and use the correct one.
- Confirm the descriptor comes from the same FileDescriptor/schema version as the code expecting the field.
Example fix
// before
var fd = msgDesc.Fields["userId"];
// after
var fd = msgDesc.FindFieldByName("user_id") ?? msgDesc.FindFieldByName("userId");
if (fd == null) { /* handle unknown field */ } Defensive patterns
Strategy: validation
Validate before calling
var fd = msgDesc.FindFieldByName(name) ?? msgDesc.Fields.ByJsonName(name);
if (fd == null) throw new InvalidOperationException($"Unknown field '{name}' on {msgDesc.FullName}"); Try / catch
try { fd = msgDesc.Fields[name]; } catch (KeyNotFoundException ex) { log.Warn($"Unknown proto field '{name}'"); fd = null; } Prevention
- Prefer FindFieldByName/ByJsonName which return null over the throwing indexer.
- Keep field-name strings in constants generated from the schema, not ad-hoc literals.
- Add integration checks after schema changes to verify all referenced field names still exist.
When it happens
Trigger: Calling messageDescriptor.Fields["someField"] where the field is not declared in the .proto message, using the JSON/camelCase name when only the proto snake_case name (or vice versa) exists, or a misspelled field name.
Common situations: Hand-written reflection code building message accessors from string names loaded from config; schema drift where a field was renamed or removed in a newer .proto version while old code still asks for it.
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
- No such field number
- FieldDescriptors can only be compared to other…
- EnumType is only valid for enum fields.
- MessageType is only valid for message fields.
- Field with message or enum type missing type_name.
AI-assisted analysis of XINCGer/Unity3DTraining@016f98412e (2026-09-12).
Data as JSON: /api/errors/3468f9bd34c1267c.
Report an issue: GitHub.
Appendix: source
Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/MessageDescriptor.cs:344
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)
{
throw new KeyNotFoundException("No such field name");
}
return fieldDescriptor;
}
}
}
}
}
View on GitHub (pinned to 016f98412e)