Search Unity

Hide "readonly" fields from the Inspector

Discussion in 'Editor & General Support' started by Peter77, Aug 7, 2019.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    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:

    1) 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?

    2) Do not display readonly field in the Inspector or display it as disabled element, so it's clear you can't change it.
     
    Last edited: Aug 7, 2019
    MrLucid72 and Lurking-Ninja like this.
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    As I posted in the other thread I was unable to recreate this issue myself. If you could share an example script or file a bug report it would be appreciated :)
     
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Crossing fingers that @MrLucid72 is able to provide a script to reproduce the issue :)