How to display specific variables in the inspector based on boolean or enum values?

Hi.

Have you ever noticed that some Unity built-in components have this particular behaviour to show some variables in the inspector only when you select a specific option, like setting the value of a boolean or a enum?

E.G. When you set “Automatic center of mass” or “Automatic Tensor” to false in the Rigidbody component, the inspector displays a new section:


I’ve always wondered how this useful feature is implemented.

I was searching some Discussions concerning this problem and the only solution that I found out was using a Custom Property Drawer. I honestly don’t know if that’s the real solution, because it seems a bit out of my scope.

Do I really need to customize the Property Drawer? I thought there was an easier way, like some Unity attributes idk

You’d need either a custom editor for any type that inherits from UnityEngine.Object, or a property drawer for anything else.

Unity doesn’t have this fuctionality built in but there are addons that can add it. Some free ones, but the best paid option is Odin Inspector.

2 Likes

Do you recommend any free addon? I feel like creating a custom editor from scratch is a bit too much for me and it slows down my workflow for the whole project

This is how it’s done:

Pretty straightforwards!

If you’re looking for a plugin that achieves this with just attributes, you can try NaughtyAttributes, which I have heard of but never used myself. But the first example they have is an [EnableIf] attribute.

1 Like