Class RequiredAttribute
Marks a serialized field as required and displays a warning message box in the Unity Inspector whenever the field's value is considered unset or empty.
Inherited Members
Namespace: Scylla.Core.Attributes
Assembly: ScyllaCore.dll
Syntax
[AttributeUsage(AttributeTargets.Field)]
public sealed class RequiredAttribute : PropertyAttribute
Remarks
The warning box is rendered above the field itself, making it immediately visible to designers and developers who have not yet assigned the required value. The decorated field remains editable; the attribute provides visual feedback only.
The following field types are supported, and the emptiness check is type-specific:
-
Object references (
UnityEngine.Objectsubclasses andExposedReference) - considered empty whennullor destroyed. -
string- considered empty whennullor whitespace-free zero-length string (string.IsNullOrEmpty). -
Managed references (
[SerializeReference]) - considered empty whennull. - Arrays and lists - considered empty when the array contains zero elements.
- All other serialized types (numeric, struct, etc.) - never considered empty; the warning is not shown.
This attribute provides Editor-only validation feedback. For runtime null-guard checks, enforce requirements with explicit code assertions or framework validation.
public class Player : MonoBehaviour
{
/* Warning: "SpawnPoint is required" displayed when not assigned */
[Required]
[SerializeField] private Transform _spawnPoint;
/* Warning with a domain-specific error message */
[Required("Player name must be set before entering a multiplayer session.")]
[SerializeField] private string _playerName;
/* Required array - warns when the array is empty */
[Required("At least one spawn location is required.")]
[SerializeField] private Transform[] _spawnLocations;
}
Constructors
RequiredAttribute()
Marks the decorated field as required. The Inspector displays a default warning message derived from the field's display name when the field is empty.
Declaration
public RequiredAttribute()
RequiredAttribute(string)
Marks the decorated field as required with a specific, user-defined warning message to display when the field is empty.
Declaration
public RequiredAttribute(string errorMessage)
Parameters
| Type | Name | Description |
|---|---|---|
| string | errorMessage | The warning text to display in the Inspector when the field has no value
assigned. Use this to provide actionable guidance, such as where to find the
asset to assign, why the field is mandatory, or what consequence an
unassigned field will have at runtime. Passing |
Properties
ErrorMessage
Gets the custom warning message displayed in the Inspector when the field's value is considered unset or empty.
Declaration
public string ErrorMessage { get; }
Property Value
| Type | Description |
|---|---|
| string | A custom error message string, or |
Remarks
When null, the Inspector drawer generates a default message from the
field's display name in the format "{FieldName} is required". Provide
a non-null string to replace the generated message with a more descriptive,
context-specific explanation.