A readonly field decorated with the [SerializeField] attribute causes Unity to display it in the Inspector. However, changes to the field through the Inspector have no effect. This came up in this thread .
In order to avoid future confusion, even though the behaviour is documented here, I suggest:
-
Emit a compile error if the [SerializeField] attribute is used on a readonly or const field. Not sure if this is possible though, perhaps through Visual Studio Tools for Unity?
-
Do not display readonly field in the Inspector or display it as disabled element, so it’s clear you can’t change it.