Class ShowIfAttribute
Conditionally shows a serialized field 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 ShowIfAttribute : ConditionalAttributeBase
Remarks
When the condition evaluates to true, the decorated field is visible
and editable in the Inspector. When the condition evaluates to false,
the field is completely removed from view. The field's value is preserved
in memory and serialization; only the visual representation is suppressed.
This is the inverse of HideIfAttribute. Use ShowIfAttribute when a field is only relevant under specific conditions and should not appear otherwise (e.g., showing projectile settings only for ranged weapons, or revealing advanced options only when an expert mode toggle is enabled).
Unlike EnableIfAttribute, which keeps the field visible but
grayed out when the condition is false, this attribute hides the field
entirely. Prefer ShowIf when there is no value in displaying the
field in its current context and reducing visual clutter is preferred.
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 Weapon : MonoBehaviour
{
[SerializeField] private bool _useProjectile;
/* Show only when _useProjectile is true */
[ShowIf(nameof(_useProjectile))]
[SerializeField] private GameObject _projectilePrefab;
[SerializeField] private WeaponType _weaponType;
/* Show only for ranged weapons */
[ShowIf(nameof(_weaponType), WeaponType.Ranged)]
[SerializeField] private float _range = 10f;
/* Show critical hit effect only when damage exceeds 50 */
[ShowIf(nameof(_attackDamage), 50, CompareOperator.GreaterThan)]
[SerializeField] private ParticleSystem _criticalHitEffect;
[SerializeField] private int _attackDamage = 25;
}
Constructors
ShowIfAttribute(string)
Shows the decorated field when the specified condition field evaluates to a truthy value.
Declaration
public ShowIfAttribute(string conditionField)
Parameters
| Type | Name | Description |
|---|---|---|
| string | conditionField | The name of the serialized field whose value controls whether this field is
shown. 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.
ShowIfAttribute(string, object)
Shows the decorated field when the specified condition field equals the given comparison value.
Declaration
public ShowIfAttribute(string conditionField, object compareValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | conditionField | The name of the serialized field whose value controls whether this field is
shown. 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.
ShowIfAttribute(string, object, CompareOperator)
Shows the decorated field when the condition field's value satisfies the given comparison operator relative to the comparison value.
Declaration
public ShowIfAttribute(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
shown. 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, show a field only 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.