Tooltips not working on property fields!

So there’s this:

[Tooltip("Works!")] public float f;

8442932--1119212--Screenshot (1).png

The tooltip is displayed by the IDE when hovering over the value: extremely useful so we don’t have to add unnecessary summaries.

But then there’s this:

[field: SerializeField][field: Tooltip("Doesn't work!")] public float F { get; set; }

No summary.

I use this quite a lot as per C# standard of using public properties instead of fields for classes. I would like for the summary to be visible in this use case as well.

EDIT:
Alternatively, I like what this guy is saying as well, having an attribute TooltipSummary
https://forum.unity.com/threads/feature-request-tooltip-displays-the-field-summary.560227/
It would solve the issue and, even better, prevent such issues in the future because you’re coming at it from the other end leaving the responsibility with

I would add to it the addition of specifying a field. That way you can do:

[SerializeField][TooltipSummary("F")] private float f; // Displays "F is used to do super cool stuff!" in the inspector.

/// <summary>F is used to do super cool stuff!</summary>
public float F
{
    get => _f;
    set
    {
        _f = value;
        DoSuperCoolStuff();
    }
}

Tooltip attributes work with SerializedProperty, not with fields. Your field is automatically serialized in your first example because it’s declared as public and belongs to a class which derives from MonoBehaviour.

Unity does not serialize C# properties, whether they’re automatic or not. You cannot use SerializeField attribute on properties. (edit3: btw that attribute applies to your next field _lookPitch, so that’s why it works)

fields vs properties in C#

When you make a custom editor/inspector, you get full access to how inspector works, and you may introduce tooltips even for random stuff like pieces of graphic and non-interactable UI elements, if you wanted.

The documentation is lousy (for the most part) and you need a lot of practice to get the hang of it. Especially SerializedObject and SerializationProperty can be tricky if you’re new to this.

edit:
It’s worth noting that fields do not have to be public to be serialized, but then you must use SerializeField to instruct Unity this should be the case.

Similarly if you don’t want a public field to be automatically serialized, you use NonSerialized attribute.

Finally if you want your field serialized but hidden in the (automatic) inspector, you can use HideInInspector.

edit2:
Oh btw, serialized “property” is a misnomer. They have nothing to do with C# properties. It’s part of the serialization API where any value that is accessible by the inspector is simply referred to as ‘property’ in plain human language.

2 Likes

whoa. okay. whatever floats your boat. good luck!

Wait are we talking about Visual Studio tooltips, or tooltips in the Unity inspector?

Because in the formers face it’s completely out of Unity’s hands.

1 Like

You’re right, this is majorly confusing now.

Great question by spiney199: what IDE?
All I saw was the picture, and Tooltip is a Unity attribute.

You don’t need to mention serialization to get a serialization-related answer. I don’t get this part of your reply.
You are using a Unity attribute in a serialization context. Whether you are aware of it or not, is something else.

If you’re talking about Unity I gave you explanation why exactly that works, but no, Unity does not serialize properties. And it’s been like this for ages, pretty much we all know these rules better than we know our parents.

In the first picture, where a field is used, you can see “Unity: Works!” in VS’s tooltip. This is inherited from the TooltipAttribute.

In the second picture, the Tooltip is attached to the field of an auto property. This shows up fine in the Unity inspector, but not in VS.

I’m not sure where the responsibility lies there then, either with Unity or whoever does the Visual Studio integration.

I honestly didn’t know that was a thing at all. I always just use /// <summary> as it’s more native, and I can include references to class definitions that I can click on and navigate to them.

1 Like

Rider has the same feature of showing [Tooltip] in the hover-tooltip, and the same bug of now showing it when targeting fields.

Working:
8444408--1119530--upload_2022-9-16_15-4-13.png

Not working:
8444408--1119533--upload_2022-9-16_15-4-36.png

Pretty sure the reason why it’s like that is because you’re not hovering the thing that’s got the tooltip, so for this to work you’d need a special case for properties to look for backing fields.

This feature request for sure belongs in Code Editors & IDEs. @John_MSFT is the Microsoft employee around there, while @van800 is the Jetbrains guy around here.

Though I mean this is a pretty esoteric use of a minor, esoteric feature, so it’ll probably not shoot up to the top of any issue trackers.

1 Like

That’s a good point, I’m a bit late to the party where “documenting my code” is concerned but yeah after some DIY it’s easy to see how powerful xml comments can be.

My efforts are now focused on doing it the other way around, writing some [TooltipXml] attribute that transforms xml comments into a tooltip. I tried to do this using reflection however for this I need to generate the .xml files but I’m having some trouble. I understand I need to go under the .csproj > Properties > Build > Output and check “Xml documentation file”, but whenever I try to go under Properties VS just kinda… blinks, and the Properties window never pops up. Anyone else having this issue?

Alternatively, I can write my comments in separate .xml files and use in my code, and for the tooltip retrieve it from that separate file instead. Since I usually only document packaged code that might not actually be that bad.