Search Unity

Access private non serialized field inside an editor script

Discussion in 'Scripting' started by mnarimani, Sep 13, 2019.

  1. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    211
    I have written a custom editor for a scriptable object. Now I want to show (and change value of) a private non-serialized field in inspector. Is it possible?
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    655
    There are ways but it wouldn't be serialized, so why?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,224
    You could use reflection, or just throw this in the target script - preferably towards the bottom:

    Code (csharp):
    1. #if UNITY_EDITOR
    2. public int PrivateValueForDebugOnly => privateValue;
    3. #endif
    Reflection is "cleaner" in that it doesn't leak info about the editor to the main script, but eh, whatever.

    @WarmedxMints it's pretty useful to show certain runtime-only values in the inspector for debugging purposes - like the current navigation target of a navigation thing, or the current played animation of something that animates things.
     
  4. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    211
    There's a field that is only used in runtime. I want designers to be able to change this field through inspector. But I don't want changes to be saved after exiting play mode
    Thanks!

    .