public field not visible in inspector

Hello guys, please i need your help.I found many threads about it and tried all i found but with no success.

I need to see my public field in inspector.

  public LayoutComponentProperties properties { get;set; }

I tried add [System.Serializable] above my class and [field:SerializeField] above my field.
This should work according to this topic : https://stackoverflow.com/questions/40554418/in-unity-can-i-expose-c-sharp-properties-in-the-inspector-window
However it is not working for me, can you please help?

Thanks in advance

Monobehaviour class attached to go :

namespace BS.Systems.UI
{
    [Serializable]
    public class ButtonComponent : MonoBehaviour, ILayoutComponent
    {
        [field:SerializeField]   
        public LayoutComponentProperties properties { get;set; }

      
        void Awake()
        {
        Init();
        }
        void Init()
        {
            properties = new LayoutComponentProperties(LayoutComponentType.button, gameObject.GetComponent<RectTransform>());
        
        }

    }
}

Other related objects:

 public enum LayoutComponentType
    {
        button, page
    }
    public class LayoutComponentProperties
    {
        public LayoutComponentType type;
        public RectTransform parent;
        public LayoutComponentProperties(LayoutComponentType type, RectTransform parent)
        {
            this.type = type;
            this.parent = parent;
        }
    }
    public interface ILayoutComponent
    {
        public LayoutComponentProperties properties { get; set; }
    }

Your “LayoutComponentProperties” class is not a serializable class ^^. You can only make fields serializable of types which are serializable. Add the System.Serializable attribute to your class.

[System.Serializable]
public class LayoutComponentProperties
{

Also as you already figured out, what you have is not a field but a property. More specifically an auto-property that automatically creates a hidden private backing field. To make that hidden field serialized you have to use

[field:SerializeField]
public LayoutComponentProperties properties { get;set; }

Though just using a public field would be simpler:

public LayoutComponentProperties properties;
1 Like

I tried this before and it is still not working.My code looks like this.
Object

  [Serializable]
    public class LayoutComponentProperties
    {
        public LayoutComponentType type;
        public RectTransform parent;
        public LayoutComponentProperties(LayoutComponentType type, RectTransform parent)
        {
            this.type = type;
            this.parent = parent;
        }
    }

Mono

        public LayoutComponentProperties properties { get; set; }

Presumably you’ve also made the private field serializable too as suggested above? The mere fact than it can be serialized obviously doesn’t mean it will be if it’s private unless you ask for that.

Or is it public now? If so then yes, it should work as you suggest.

Hello it is public, i provided screenshot in reply above.

As mentioned above, that’s a property, not a field. The inspector doesn’t show properties. If you serialize the backing field for the property then it’ll show the field.

The inspector will do this but it has dedicated serialized properties you can use here for editor related stuff.

1 Like

Thanks for reply i will look in to that.

Thank you it is working now.

I am using [Serializable] for LayoutComponentProperties class and [field: SerializeField] for

   [field: SerializeField]
    public LayoutComponentProperties properties { get; set; }