Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Types that need to show up in the inspector

Discussion in 'Editor & General Support' started by Warsymphony, Nov 6, 2015.

  1. Warsymphony

    Warsymphony

    Joined:
    Sep 23, 2014
    Posts:
    43
    int? myInt = null; This variable and its value should show up in the inspector.

    So should this
    public int myInt { get; set; }

    Lets get this implemented also Unity staff. Would be nice if Unity used C# 6.0
    public int myInt { get; set; } = 5;
     
  2. Brainswitch

    Brainswitch

    Joined:
    Apr 24, 2013
    Posts:
    270
    Agreed, that would be nice.
    With some fidgeting you can get them to show up via custom inspectors/editors, but getting them serialized is harder.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    According to Unity's rules, only types that are serializable by its serialization system are shown in the inspector (by default).

    This is just syntactic sugar for a Nullable<int> type - a value type that holds a value and a flag to denote whether it's actually holding a value or not.

    This is not a "variable" as you call it, but a property. After compilation, this get converted into a method, this is not a field access (again, this is syntactic sugar for making it more "natural" to access fields + being able to add custom logic to accessing them, controlling their protection level, etc).

    None of these options are derived from UnityEngine.Object or is marked with [Serializable] so there's no reason why they should appear in the inspector for now.

    I do agree though, that there are probably some missing stuff that would've been nice if they did show up in the inspector.
     
  5. Warsymphony

    Warsymphony

    Joined:
    Sep 23, 2014
    Posts:
    43
    public int? myInt = null;

    ^ This is a field, it is public, it is not static, it is not a property. According to Unity's rules this should be serializable and show up in the inspector.

    public int myInt { get; set; }

    ^ This may compile to a to a method but it still has a field. Even though the field cannot be accessed directly there is no reason Unity cannot get that field and display it.

    There are some times I absolutely need to be able to watch the value of a variable while debugging. So I find myself doing this.

    public float myInt {get; set;}
    public float myIntForDispaly;

    Then in Update() ill add

    myInt = transform.position.x;
    myIntForDispaly = myInt;

    Not that I would do this to view transform.position.x just an example

    This is silly to have to do there is no reason Unity cannot handle this in the background so fields generated by auto properties can be viewed in the inspector.

    Unity may only display fields that are serialized however it would be nice if it could display private fields, nullable types, static fields, and fields generated by auto properties.
     
  6. Warsymphony

    Warsymphony

    Joined:
    Sep 23, 2014
    Posts:
    43
    I find myself using the inspector to watch values for debugging not just changing values during runtime. Perhaps I am just using the inspector incorrectly.
     
  7. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    Where did you see those rules? they only serialize primitive types, or anything that's derived from UnityEngine.Object.
    If you define your own class and put it as a public field it also won't get listed.. (you have to mark it explicitly as serializable).

    I am just describing things as they are RIGHT NOW... that's not to say that i don't think they should improve it. I'm totally with you with supporting properties and other types (e.g: Dictionaries)
     
  8. Warsymphony

    Warsymphony

    Joined:
    Sep 23, 2014
    Posts:
    43
    Yes dictionaries too, that would be amazing!
     
  9. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,984
  10. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    You can create an inspector of your own to display properties... shouldn't be too difficult.
     
    zombiegorilla likes this.
  11. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,984
    Yea, you can do that too. the nice thing about property drawers is that you don't need to do a full inspector for each class. They will automatically display the property every where it is declared.
     
    liortal likes this.
  12. Warsymphony

    Warsymphony

    Joined:
    Sep 23, 2014
    Posts:
    43
    Ah, yes, property drawers let's get that fixed too. I had to quit using these as they have a tendency to not always show. Especially after resizing the inspector window.
     
  13. Warsymphony

    Warsymphony

    Joined:
    Sep 23, 2014
    Posts:
    43
    I'm glad you brought up property drawers I think I'll give that another go. It was a few unity versions ago that I was having display issues.