Class NotNullAttribute
Marks a field as requiring a non-null reference, enforced at runtime by ValidateObject(object).
Inherited Members
Namespace: Scylla.Core.Attributes
Assembly: ScyllaCore.dll
Syntax
[AttributeUsage(AttributeTargets.Field)]
public sealed class NotNullAttribute : Attribute
Remarks
Unlike RequiredAttribute, which surfaces a warning box in the Unity
Inspector at edit-time only, NotNullAttribute is evaluated programmatically
at runtime. This makes it suitable for catching missing references in builds where
the Inspector is unavailable, and aligns with Scylla's fail-fast philosophy of
surfacing configuration errors before any gameplay logic executes.
The validator correctly handles Unity's pseudo-null pattern. A
UnityEngine.Object-derived field that has been destroyed at runtime
will pass a C# null check (== null) but still fail the Unity equality
operator. AttributeValidator uses the Unity equality operator for
such fields, so destroyed Unity objects are correctly reported as null.
This attribute targets fields only (Field) and is picked up through reflection by ValidateObject(object). Both public and non-public (private, protected) fields are inspected.
To check for emptiness in addition to null (e.g., for strings or collections), use NotEmptyAttribute. For custom validation logic, use ValidateInputAttribute.
public class GameManager : MonoBehaviour
{
[NotNull]
[SerializeField] private Camera _mainCamera;
[NotNull("AudioManager reference is required!")]
[SerializeField] private AudioManager _audioManager;
private void Awake()
{
/* Throws ValidationException if _mainCamera or _audioManager is null */
AttributeValidator.ValidateObject(this);
}
}
Constructors
NotNullAttribute()
Initializes a new instance of NotNullAttribute that uses the default generated error message when validation fails.
Declaration
public NotNullAttribute()
Remarks
When the decorated field is null at validation time, AttributeValidator
produces the message "Field '{fieldName}' cannot be null." automatically,
where fieldName is the C# name of the annotated field as returned by
reflection.
NotNullAttribute(string)
Initializes a new instance of NotNullAttribute with a custom error message that is used verbatim when validation fails.
Declaration
public NotNullAttribute(string errorMessage)
Parameters
| Type | Name | Description |
|---|---|---|
| string | errorMessage | The human-readable message to include in the thrown ValidationException
when the field value is |
Remarks
The supplied errorMessage is stored verbatim and forwarded to
ValidationException without modification. There is no validation of
the message string itself; passing null here is equivalent to using the
parameterless constructor.
Properties
ErrorMessage
Gets the custom error message displayed when validation fails for the decorated field.
Declaration
public string ErrorMessage { get; }
Property Value
| Type | Description |
|---|---|
| string | A non-null string containing the developer-supplied error message, or |