Devlog: Immutable Inspector Fields

Inspired by a question posted on Reddit (Read Only Parameter with SerializeField), I realized that I don’t have a way of communicating to other developers on my team which fields are not designed to be modified while playing in the editor! So I decided to solve this problem by adding a new attribute [Immutable] which disables fields in the inspector.

The attribute has two usage patterns: [Immutable], which causes the field to become greyed-out (disabled) when the editor is running, and [Immutable(false)], which causes the field to be greyed-out always (the optional parameter is called isEditable). The attribute is very simple, but useful. Use it well! You might also find this a nice way to learn the syntax for writing your own attributes and property drawers if you haven’t explored that yet.


[SerializeField] [Immutable] private int _inventorySize = 10;
[SerializeField] [Immutable(false)] private int _inventorySearchCount = 3;

This first example is indicative of a system which initializes the inventory system when Awake is called (instantiating enough slots, etc). We don’t want anyone changing this at runtime from inside the editor.

The second is something I wanted to be able to do to protect myself from forgetting how something works without exposing the system to additional dangers. Imagine a search feature that looks through the inventory in some potentially CPU-heavy way. I want to limit how many times this code executes before giving up, but I don’t want to hard-code the number and forget it exists, or have to open the code to find out. Still, I don’t want anyone to change it without know exactly what they’re doing. This forces future-me to go into the code to change the value, but I won’t forget it exists.

Application is not playing.
Application is not playing.
Application is playing.
Application is playing.


7 thoughts on “Devlog: Immutable Inspector Fields”

  1. Just wondering what the process is to actually implement this in one of my scripts. Do I need to make any using references? Assign something? This is a great product and I would love to get it working 🙂


    1. If you put the provided scripts into your project, you should have access to an attribute [Immutable] that you can preface your fields with. I did not put the attribute in a namespace, so it should be globally accessible. You can change that if you’d like. 🙂

      I’ll mention that the [Immutable] attribute does not work on arrays or lists; I never got around to that. Still, pretty handy.

      Generally I have things like this in my scripts:

      #region Inspector Variables
      [SerializeField] [Immutable] private SomeComponentType _someCompenent;

      1. Well that is what I tried to do at first…
        But I kept getting missing assembly reference error..
        Then I decided maybe I should sync MonoDevelop.
        And now it works!!

        Thanks so much man!

  2. Could you change the article to say you need [ImmutableAttribute] instead of [Immutable]. Confused me at first.

Comments are closed.