Class EnableIfAttribute
Conditionally enables a serialized field for editing in the Unity Inspector based on the current value of another field on the same object.
Inherited Members
Namespace: Scylla.Core.Attributes
Assembly: ScyllaCore.dll
Syntax
[AttributeUsage(AttributeTargets.Field)]
public sealed class EnableIfAttribute : ConditionalAttributeBase
Remarks
When the condition evaluates to true, the decorated field is active
and editable. When the condition evaluates to false, the field is
grayed out and cannot be modified, but remains visible in the Inspector so
its current value is still observable.
This is the inverse of DisableIfAttribute. Use EnableIfAttribute when a field should normally be read-only but becomes editable only under specific circumstances (e.g., when a feature toggle is enabled, or when an advanced mode is active).
Unlike ShowIfAttribute, which hides the field entirely when
the condition is false, this attribute always keeps the field visible.
Prefer EnableIf when the field's current value still conveys useful
information even while editing is restricted.
The condition field must be serialized and accessible from the same object. The attribute supports the following field types for condition evaluation:
bool- truthy whentrueint- truthy when non-zerofloat- truthy when non-zerostring- truthy when non-null and non-emptyenum- truthy when the value is not the first enum member (index 0)- Object reference - truthy when the reference is not
null
Use nameof() for the condition field name to get a compile-time-safe
string that refactoring tools can update automatically.
public class AudioSettings : MonoBehaviour
{
[SerializeField] private bool _enableAudio = true;
/* Editable only when audio is enabled */
[EnableIf(nameof(_enableAudio))]
[SerializeField] private float _masterVolume = 1f;
[EnableIf(nameof(_enableAudio))]
[SerializeField] private float _musicVolume = 0.8f;
[SerializeField] private AudioQuality _quality;
/* Enable advanced options only when quality is set to High */
[EnableIf(nameof(_quality), AudioQuality.High)]
[SerializeField] private bool _use3DAudio = false;
/* Enable when reverb sends exceed a threshold */
[EnableIf(nameof(_reverbSends), 2, CompareOperator.GreaterThanOrEqual)]
[SerializeField] private float _reverbBlend = 0.5f;
[SerializeField] private int _reverbSends = 0;
}
Constructors
EnableIfAttribute(string)
Enables the decorated field for editing when the specified condition field evaluates to a truthy value.
Declaration
public EnableIfAttribute(string conditionField)
Parameters
| Type | Name | Description |
|---|---|---|
| string | conditionField | The name of the serialized field whose value controls whether this field is
enabled. Use |
Remarks
Truthiness is determined by the field type: true for booleans,
non-zero for numeric types, non-null and non-empty for strings, non-null for
object references, and non-zero index for enums. This overload does not
perform an explicit value comparison - it evaluates the field as a boolean
condition directly.
EnableIfAttribute(string, object)
Enables the decorated field for editing when the specified condition field equals the given comparison value.
Declaration
public EnableIfAttribute(string conditionField, object compareValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | conditionField | The name of the serialized field whose value controls whether this field is
enabled. Use |
| object | compareValue | The value to compare against the condition field. Supports |
Remarks
The comparison uses Equals by default.
The compareValue is compared against the condition
field's value using type-appropriate equality: boolean equality for
bool fields, Mathf.Approximately for float fields,
ordinal string comparison for string fields, and integer index
comparison for enum fields.
EnableIfAttribute(string, object, CompareOperator)
Enables the decorated field for editing when the condition field's value satisfies the given comparison operator relative to the comparison value.
Declaration
public EnableIfAttribute(string conditionField, object compareValue, CompareOperator op)
Parameters
| Type | Name | Description |
|---|---|---|
| string | conditionField | The name of the serialized field whose value controls whether this field is
enabled. Use |
| object | compareValue | The value to compare against the condition field. Must be compatible with
the chosen |
| CompareOperator | op | The CompareOperator used to evaluate the condition. Determines
how the condition field's value is compared against |
Remarks
Use this overload when a simple equality check is insufficient. For example, enable a field only when a numeric value falls within a range, or when a flags enum contains a specific combination of bits.
Ordered comparisons (LessThan,
GreaterThan, etc.) are supported for
int and float fields only. Bitwise AND comparison
(BitwiseAnd) is supported for int
and enum (flags) fields.