Version 3.3.1.2

Odin has a dedicated attribute overview with examples

ReadOnlyAttribute class

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector.Attributes
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | AttributeTargets.All, AllowMultiple = false, Inherited = true)]
[Conditional("UNITY_EDITOR")]
public sealed class ReadOnlyAttribute : Attribute, _Attribute

ReadOnly is used on any property, and prevents the property from being changed in the inspector.

Use this for when you want to see the value of a property in the inspector, but don't want it to be changed.

Inheritance
  • System.Object
  • System.Attribute
  • ReadOnlyAttribute
Remarks

note

This attribute only affects the inspector! Values can still be changed by script.

Example

The following example shows how a field can be displayed in the editor, but not be editable.

public class Health : MonoBehaviour
{
	public int MaxHealth;

	[ReadOnly]
	public int CurrentHealth;
}
Example

ReadOnly can also be combined with ShowInInspectorAttribute.

public class Health : MonoBehaviour
{
	public int MaxHealth;

	[ShowInInspector, ReadOnly]
	private int currentHealth;
}

Constructors

ReadOnlyAttribute()
public ReadOnlyAttribute()