Class DisableIfAttribute
Conditionally disables 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 DisableIfAttribute : ConditionalAttributeBase
Remarks
When the condition evaluates to true, the decorated field is grayed
out and cannot be modified in the Inspector. The field remains visible so
designers and developers can still observe its current value.
This is the inverse of EnableIfAttribute. Use DisableIfAttribute when a field should normally be editable but must be locked under specific circumstances (e.g., when a configuration is locked, or when the game is in a particular state).
Unlike HideIfAttribute, which removes the field from view
entirely, this attribute keeps the field visible while preventing
modification. Prefer DisableIf when the current value still conveys
useful information to the user.
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 LockableSettings : MonoBehaviour
{
[SerializeField] private bool _isLocked;
/* Disabled whenever _isLocked is true */
[DisableIf(nameof(_isLocked))]
[SerializeField] private string _configValue;
[DisableIf(nameof(_isLocked))]
[SerializeField] private int _maxPlayers = 4;
[SerializeField] private GameState _state;
/* Disable when the game state equals GameState.Playing */
[DisableIf(nameof(_state), GameState.Playing)]
[SerializeField] private float _difficulty = 1f;
/* Disable when the damage value exceeds 50 */
[DisableIf(nameof(_damage), 50, CompareOperator.GreaterThan)]
[SerializeField] private bool _allowOneShotKills = false;
[SerializeField] private int _damage = 25;
}
Constructors
DisableIfAttribute(string)
Disables the decorated field for editing when the specified condition field evaluates to a truthy value.
Declaration
public DisableIfAttribute(string conditionField)
Parameters
| Type | Name | Description |
|---|---|---|
| string | conditionField | The name of the serialized field whose value controls whether this field is
disabled. 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.
DisableIfAttribute(string, object)
Disables the decorated field for editing when the specified condition field equals the given comparison value.
Declaration
public DisableIfAttribute(string conditionField, object compareValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | conditionField | The name of the serialized field whose value controls whether this field is
disabled. 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.
DisableIfAttribute(string, object, CompareOperator)
Disables the decorated field for editing when the condition field's value satisfies the given comparison operator relative to the comparison value.
Declaration
public DisableIfAttribute(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
disabled. 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, disable a field when a numeric value exceeds a threshold, 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.