Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Access private non serialized field inside an editor script

Discussion in 'Scripting' started by Deleted User, Sep 13, 2019.

  1. Deleted User

    Deleted User

    Guest

    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:
    1,035
    There are ways but it wouldn't be serialized, so why?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    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. Deleted User

    Deleted User

    Guest

    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!

    .